即使服务器响应是带有有效JSON数据的HTTP 200,JQuery Ajax调用也会转到.fail

时间:2015-02-01 04:11:10

标签: jquery ajax json

为什么这个JQuery代码失败的任何想法?无论我尝试什么,它总是去.fail,而不是.done(或成功)。报告的错误非常通用(statustext =“error”)。

ajax调用正确地将数据发送到服务器,并且服务器响应使用有效的json数据成功。这是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!');
        });
    }

这是HTTP请求(来自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"}

以下是HTTP响应:

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

欢迎任何想法!

1 个答案:

答案 0 :(得分:3)

您尝试访问的服务器不允许跨源请求。

服务器必须具有以下标题:

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

最终,你可以找到一个代理。

可在此处找到更多信息:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS