rafale
rafale

Reputation: 1735

jQuery POST does not send JSON data

I'm trying to do a POST to a service running on localhost with jQuery AJAX, but it keeps returning status code 0 even after I've set jQuery.support.cors = true. I can also navigate to my WCF REST service successfully from my browser. This is what my JavaScript looks like:

    <script>
        jQuery.support.cors = true;
        $(document).ready(function(){
            $.ajax({
                type: "POST",
                url: "http://localhost:8000/Test",
                data: '{"test":"test"}',
                contentType: "application/json",
                dataType: "json",
                success: function (msg) {
                    alert('success');
                },
                error:function(x,e){
                    if(x.status==0){
                        alert('error 0');
                    }
                }
            });
        });
    </script>

Does anyone know what could be causing this? I should also mention that I can't POST to anything on localhost using jQuery.

According to Fiddler, the JSON data is not sent, and a HTTP OPTIONS is done instead of a POST.

Upvotes: 2

Views: 7624

Answers (5)

dudewad
dudewad

Reputation: 13933

So, when the request is cross domain, jQuery will send your post request as a get request anyways. Are you accessing "localhost" in the URL but then your application is sending the requests to the local IP of your machine instead of localhost? Because that's technically cross-domain, which means that you won't receive the request in the expected manner.

E.g. (just tested this locally) Visiting my local site at:

http://localhost/test/

A form on the site submits to my local ip address instead of localhost via $.post():

<form action="http://10.0.0.17/test/" method="post">
   ....[form stuff...]
</form>

This is a cross-domain request

If you're calling $.post() or jquery's ajax() call set to post, it automatically moves your parameters from the post body into the query string.

If you ARE accessing local host, try hitting the site via whatever address your jquery post() method is using as the domain and see if that helps.

See more on cross-domain policies: http://en.wikipedia.org/wiki/Same_origin_policy

Upvotes: 1

peteski
peteski

Reputation: 1523

You should use a tool like network monitor etc. to see if the browser is asking the server for the allowed headers (using the OPTIONS header request), you may need to supply the correct headers in an OPTIONS response before the actual request is sent to the server (see the article at the bottom).

Also, you could try adding this to the actual call or the ajaxSetup, as you will need to tell the browser to send credentials and allow the cross domain call (I know someone else already mentioned 'crossDomain'):

$.ajaxSetup({
  crossDomain: true,
  xhrFields: {
    withCredentials: true
  }
});

Have a read of this if you get time too.. https://developer.mozilla.org/en/http_access_control

Upvotes: 2

rafale
rafale

Reputation: 1735

I didn't want to spend anymore time on this issue, so I resorted to using raw HTML form POST as the usage of JSON wasn't essential in my case.

For anyone else having the same issues outlined in the original post, see this thread for an explanation and a solution: Problem sending JSON data from JQuery to WCF REST method

To summarize, your service needs to be able to handle the HTTP OPTIONS method if it is expected to respond to cross domain calls.

Upvotes: 2

user554180
user554180

Reputation:

try this

var dataObj = {test:"test"};

var json = JSON.stringify(dataObj);

then in your ajax call

data: json,

Upvotes: 5

Marlin
Marlin

Reputation: 749

Send the data as an Object literal instead of a string

data: '{"test":"test"}',

to

data: {test:"test"},

Upvotes: -2

Related Questions