0

I need to send form data to an API upon submission of the form. The API accepts the data in the URL itself. For example, a URL should look something like this...

"http://client_server&function=add_lead&phone_number=07875582533&title=Mr&first_name=Nicholas&last_name=Dunk&phone_number=07574657463"

The phone number and name etc are generated from the form.

If I copy the API URL into the browser with form data I get a simple "SUCCESS: Lead has been added" response, so I know it's working.

I'm fairly new to JS so I don't really know how to go about doing this. I've tried using a $.get() request, but it gives me a "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin null is therefore not allowed access" error. I understand that this is a cross origin request, so I tried a jsonp request but this gives me an "uncaught syntax error: unexpected identifier" error. This only occurs if the datatype is changed to jsonp, so I assume it has some issue with the ?callback=?... that's added for jsonp.

The only other solution I have come up with is to open the URL in a new window on submission of the form, and then immediately close it again with JS, but this is not an ideal solution.

Can anyone help?

Thanks

Olly B
  • 121
  • 10
  • Possible duplicate of ["No 'Access-Control-Allow-Origin' header is present on the requested resource"](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) – Matteo Piano Feb 05 '17 at 14:41
  • Unless that api supports jsonp format, and you make a jsonp request, you will need a proxy to access it – charlietfl Feb 05 '17 at 14:43
  • I tried a jsonp request, but I get an 'uncaught syntax error, unexpected identifier' error. This only occurs when I try a jsonp request, so I assume their server is not set up to support this? It seems to have a problem with the '?callback=?...' that jsonp adds – Olly B Feb 05 '17 at 15:18

0 Answers0