Quick question regarding $.post() and APIs

Quick question regarding $.post() and APIs
0.0 0

#1

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…
"http://client_server&function=add_lead&phone_number=07875582533&title=Mr&first_name=Nicholas&last_name=Dunk&phone_code=44"
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?
e.g.
$.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!


#2

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.


#3

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!


#4

@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.


#5

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.


#6

Why would sending data change server state? :confused:

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


#7

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


#8

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?

Thanks


#9

Hi @obolland

I’d suggest you read those two books (they cover the very minimum a web dev should know):
https://launchschool.com/books/http
https://launchschool.com/books/working_with_apis

Also, on the subject itself:
https://dev.socrata.com/docs/cors-and-jsonp.html


#10

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?!


#11

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: