Quick question regarding $.post() and APIs

Hi guys, just a quick question.

I’m setting up a form for a client which posts the form data to an API. In this case the API wants the data in the URL itself.
For example the URL should looks something like this…
The phone number and name etc are generated from the form.

My question is, do I use a POST request and just not include any additional data?
$.post(URL, function(status){ window.alert(status); )}
status being the success/fail response from the server.

I’ve not used post before so this is new to me.
Thanks for your help!

Hi @obolland

It depends on how the server is setup, but generally speaking, yes, if they expect it as a query string and server is expecting a post request, that is how you want to do it.

Thanks @joesmith100.

The only info I’ve got from their documentation is…
“Requesting a lead to be added to the dialler is fairly straight forward, all it requires is the information to be formatted into a URL string and then triggered.”

No mention of whether or not the server is expecting a post request.
I’m still waiting on a password so I guess I’ll find out when I start playing around with it!

@obolland I’ve just had a look at this http://www.w3schools.com/tags/ref_httpmethods.asp

Judging by that, it looks like they require a GET request making use of the query string of the URL.

ahh yes, that’s exactly what I was looking for. Thanks @JohnnyBizzel :slight_smile:

edit Having read some more…apparently if a get request causes a change of server state (which in this case I suppose it would given that I am sending data) then get should not be used. I guess I’ll try them both, if post works then it’s a little more secure and doesn’t restrict the length of the URL.

Why would sending data change server state? :confused:

POST requests will be always used on a log in form.

I think I misunderstood the definition of a change in server state!
You’re right, $.get looks the way to go :slight_smile:

Ok so I have the passwords etc but no luck with .$get().
I get error… “No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin null is therefore not allowed access”.

If i copy the API URL into a browser and hit enter it works fine. I get a simple response displayed in the browser saying “SUCCESS: Lead has been added…”.

Anyone have any idea how I go about this?


Hi @obolland

I’d suggest you read those two books (they cover the very minimum a web dev should know):

Also, on the subject itself:

Thanks @marzelin.
I understand the cross origin policy, I have tried a jsonp request but their server doesn’t seem to be set up to support it.
Perhaps the only way is to trigger a link to the URL and immediately close the window?!

Origin null tells me that you’re trying to connect to the api from a page opened from your local hard drive.
Your client’s api may block any cross-origin requests (since they connect to the api only from their own domain).

For your local development purposes, you can disable sop in your browser, ie for chrome: