-2

I'm using a jquery ajax for storing a data in the database. I call a service through a url and send data to it in the following way:

$.ajax({
                        type: "POST",
                        url: "http://192.168.250.118:8080/rest_service/rest/user",
                        data: JSON.stringify({ "loginName": "tsample@gmail.com", "mobile": "1234567890" }),
                        async: false,
                        contentType: "application/json",
                        crossDomain: true,
                        success: function(data){
                            alert("success");
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown){
                            console.log(XMLHttpRequest);
                            console.log(textStatus);
                            console.log(errorThrown);
                        }
                    });

When I execute this, I get this error:

{"readyState":0,"status":0,"statusText":"error"}

This is what I get when I look for errors in network:

enter image description here

But, if I try using postman, the data is getting stored and I get 200OK response. What's wrong with the above code? What should I change?

SSS
  • 663
  • 4
  • 10
  • 19
  • 2
    Change alert(response.status) to console.log(response) and let the browser tell you the error. Should you then still need help, post the error – baao Jan 31 '17 at 08:39
  • 1
    0 is success or error alert ? – Freddy Sidauruk Jan 31 '17 at 08:40
  • read old so question http://stackoverflow.com/questions/2000609/jquery-ajax-status-code-0 – Shree Jan 31 '17 at 08:41
  • BTW: You can use the network tab of the dev tools in modern browsers to directly look at the response from your rest API. – Rob Jan 31 '17 at 08:42
  • @FreddySidauruk: error alert. – SSS Jan 31 '17 at 08:43
  • @baao: this is what I get: `Object { readyState: 0, getResponseHeader: .ajax/x.getResponseHeader(), getAllResponseHeaders: .ajax/x.getAllResponseHeaders(), setRequestHeader: .ajax/x.setRequestHeader(), overrideMimeType: .ajax/x.overrideMimeType(), statusCode: .ajax/x.statusCode(), abort: .ajax/x.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 10 more… }` – SSS Jan 31 '17 at 08:45
  • @SSS try `console.log(JSON.stringify(response));` it'll probably be more readable. Also be aware that the "error" callback has two other parameters: textStatus and errorThrown, which are both strings - see the docs at http://api.jquery.com/jquery.ajax/ – ADyson Jan 31 '17 at 08:47
  • 1
    Again, post the error. Edit your question and add it. What you have commented is not the error, but the response object. – baao Jan 31 '17 at 08:47
  • 1
    And put the output in a code block in your question, not as a comment please. – Ofir Baruch Jan 31 '17 at 08:47
  • Is there any CORS related error in the console? Something similar to `Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin` – Developer Jan 31 '17 at 08:59
  • @Developer: I don't think so. That is why I've provided `crossDomain: true`. – SSS Jan 31 '17 at 09:10
  • setting `crossDomain: true` doesn't mean you can escape CORS issue. The response headers should have valid CORS headers to get this working. If you dont have CORS related error in network tab/console, then this might be some other issue. – Developer Jan 31 '17 at 09:15
  • @Developer: It doesn't show anything. I get the above mentioned error only. Even in network it doesn't show CORS error thing. – SSS Jan 31 '17 at 09:49
  • try `data: JSON.stringify({ "loginName": "tsample@gmail.com", "mobile": "1234567890" })`. Also get ther entire error details - `error: function( jqXHR, textStatus, errorThrown ){}` and check whether you have any usable info in `errorThrown` – Developer Jan 31 '17 at 10:04
  • Content-Type should be "application/json", but in your request its going as form-urlencoded. try `data: JSON.stringify({ "loginName": "tsample@gmail.com", "mobile": "1234567890" })` and set `contentType:"application/json" in the ajax call. – Developer Jan 31 '17 at 12:33
  • @Developer: I tried as how you told. But, same result. See the above edited code. – SSS Jan 31 '17 at 12:44
  • still you havnt changed the data - `data: JSON.stringify({ "loginName": "tsample@gmail.com", "mobile": "1234567890" })` – Developer Jan 31 '17 at 12:46
  • @Developer: Sorry didn't show that. Check it out now – SSS Jan 31 '17 at 12:48
  • How does your network tab values look now? Please update that as well. Also in your code its `http` but in network tab its `https` - is that a typo? – Developer Jan 31 '17 at 12:48
  • @Developer: Have updated that too. – SSS Jan 31 '17 at 12:51
  • voila...now you got the CORS issue I guess :) – Developer Jan 31 '17 at 12:52
  • @Developer: How do you say that? – SSS Jan 31 '17 at 13:35
  • I won't say it threw error, but CORS has triggered - check the preflight `OPTIONS` request being sent – Developer Jan 31 '17 at 14:23
  • Which means the call is made fine now and your original issue is resolved. – Developer Jan 31 '17 at 14:42

1 Answers1

0

Stringify the data before sending - data: JSON.stringify({ "loginName": "tsample@gmail.com", "mobile": "1234567890" }) amd contentType:"application/json"

Developer
  • 5,545
  • 2
  • 16
  • 24