Doug
Doug

Reputation: 23

JQuery Ajax call going to .fail even though server response is HTTP 200 with valid JSON data

Any ideas why this JQuery code is failing? No matter what I try, it always goes to .fail, not .done (or success). The error reported is very generic (statustext = "error").

The ajax call is properly sending data to the server, and the server response is successful with valid json data. Here is the Javascript:

function submitform()
    {       
        $.ajax({
          type: "POST",
          url:'http://192.168.1.73:8080/LicenseService/v1/license',
          data: getData()
        }).done(function(data) {
          alert('success');
        }).fail(function(data) {
          alert('fail!');
        });
    }

Here is the HTTP request (from Fiddler)

POST http://192.168.1.73:8080/LicenseService/v1/license HTTP/1.1
Host: 192.168.1.73:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 112
Origin: null
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

{"licensetype":"com.test.testlicensetype","ExpiryDate":"2015-12-15","id":"alksjdfojasdoifjsaoid"}

Here is the HTTP response:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 01 Feb 2015 04:00:03 GMT

50
{ "LicenseKey" : "VHVlIERlYyAxNSAwMDowMDowMCAyMDE1YWxrc2pkZm9qYXNkb2lmanNhb2lk"}
0

Any ideas are welcome!

Upvotes: 2

Views: 799

Answers (1)

Ismael Miguel
Ismael Miguel

Reputation: 4241

The server you are trying to access doesn't allow Cross-origin requests.

The server must have the following headers:

access-control-allow-origin: <server name or *>
access-control-allow-methods: POST
access-control-allow-headers: content-type, accept
access-control-max-age: 10

Ultimately, you can find a proxy for this.

More information can be found here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Upvotes: 3

Related Questions