views:

44

answers:

1

I'm setting up an API for a simple webservice and stumbled upon a problem. I use Ruby on Rails for the back end and a JQuery ajax call to send a POST to create an instance of a model. The JavaScript code is:

$.ajax({
   type: "POST",
   url: "http://localhost:3000/api/v1/person/add",
   data: "name=John&location=Boston",
 success: function(msg){
    alert( "Data Saved: " + msg );
}
});

When I execute this to the server I get a request type of OPTION, which I think is the first call of the two needed to complete a POST request(?) Because these sites are not on the same domain (since calls are gonna be made remotely) it fails. The main problem however is that the request itself does not contain any data at all in the body. My guess is that this first call is not sending any data but waits for some sort of 'OK' to be able to send the data?? If not, why is it empty? I don't want to use the GET request since it says in the guidelines of a RESTful API that it should use POST for this purpose, plus I might want to send more data than GET can handle. So in case my assumptions are correct and this POST request fails because of cross-domain calls, what are my options?

+2  A: 

Edit: Now I see that you're trying to do cross-domain Ajax queries... that's a non-starter. See here for additional info (just searched cross domain and picked one): http://stackoverflow.com/questions/2558977/ajax-cross-domain-call

Fosco
What has has works as well...what you have gets serialized into the same string.
Nick Craver
@Nick ah, nice to know, thanks.. edited my question.
Fosco
Does this only go for POST requests?
miccet
Fosco
I can call it with a GET method though, why does that work?
miccet