Jquery AJAX POST调用返回200状态OK但错误

时间:2013-07-10 16:17:20

标签: javascript web-services jquery

您好我已经实现了一个Ajax POST请求来调用Web服务。它总是返回200 OK但执行失败的事件我已经尝试了很多东西,但我没有得到我在做错的地方。我在这里添加我的代码。 Fiddler显示响应内容,但Ajax没有执行成功事件。

<!DOCTYPE html>
<html><head>
<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">

$.ajax({
    type: "POST",
    url: url,
    data: values,
    dataType: 'json',
    cache: false,
    success: function (result) {
        alert("success:"+result);
    },
    error:function (error) {
        alert("error"+error);
    }
});
</script>
</body>
</html>

我尝试过dataType:'text'....来自服务器的响应是正确的JSON格式

请求JSON数据:

{
    "search": {
        "params_attributes": {
            "adults": "1",
            "children": "0",
            "depart_date": "2013-07-10",
            "destination_name": "OMS",
            "direct": "0",
            "infants": "0",
            "origin_name": "KUL",
            "range": "0",
            "trip_class": "0"
        }
    },
    "signature": "XXXXXXXX",
    "marker": "XXX"
}

响应JSON数据:

{
    "search_id":"66",
    "metadata" : {},
    "tickets" : [
        {
            "native_prices":{"7":"5500.0", "6":"5680.0", "3":"5657.0"},
            "order_urls":{"7":"1","6":"557","3":"906"},
            "direct_flights":[
                {
                    "number":"1837",
                    "airline":"FV",
                    "departure":"1294037100",
                    "arrival":"1294041900",
                    "duration":"80",
                    "delay":"0",
                    "origin":"DME",
                    "destination":"LED",
                    "aircraft":"Airbus A319"
                }
            ],
            "return_flights":[
                {
                    "number":"1858",
                    "airline":"FV",
                    "departure":"1295503800",
                    "arrival":"1295508600",
                    "duration":"80",
                    "delay":"0",
                    "origin":"LED",
                    "destination":"DME",
                    "aircraft":"Airbus A320"
                }
            ]
        }
    ],
       "airlines": {
          "AB": {
            "alliance_name": null,
            "average_rate": 3.84,
            "deeplink_id": 18,
            "homepage_id": "4ec0ff7b9f1c2760af0049d7",
            "id": 35,
            "name": "airberlin",
            "rates": 377
          },
          "AF": {
            "alliance_name": "SkyTeam",
            "average_rate": 2.89,
            "id": 48,
            "name": "Air France",
            "rates": 292
          }
        },
    "airports":{
          "ALA": {
            "average_rate": 3.66,
            "city": "\u0410\u043b\u043c\u0430\u0442\u044b",
            "country": "\u041a\u0430\u0437\u0430\u0445\u0441\u0442\u0430\u043d",
            "name": "\u0410\u043b\u043c\u0430\u0442\u044b",
            "rates": 60,
            "time_zone": "Asia/Almaty"
          },
          "AMM": {
            "average_rate": 3.42,
            "city": "\u0410\u043c\u043c\u0430\u043d",
            "country": "\u0418\u043e\u0440\u0434\u0430\u043d\u0438\u044f",
            "name": "Queen Alia International",
            "rates": 5,
            "time_zone": "Asia/Amman"
          }
    },
    "currency_rates":{"eur":"41.3564", "uah":"3.72911"},
        "gates_info": [
          {
            "average_rate": 4.34,
            "currency_code": "rub",
            "id": 1,
            "is_airline": false,
            "label": "Nabortu",
            "mobile_version": false,
            "payment_methods": [
                "bank",
                "yandex_money",
                "web_money",
                "terminal",
                "card",
                "svyaznoy",
                "euroset"
            ],
            "rates": 336
          },
          {
            "average_rate": 4.34,
            "currency_code": "rub",
            "id": 2,
            "is_airline": false,
            "label": "Davs",
            "mobile_version": false,
            "payment_methods": [
                "cash",
                "bank",
                "yandex_money",
                "web_money",
                "terminal",
                "card",
                "exp",
                "euroset"
            ],
            "rates": 416
          }
        ]
}

Ajax错误:     { “readyState的”:0 “responseText的”: “”, “状态”:0 “状态文本”: “错误”}

我正在添加从firebug中截取的截图请到tinygrab网址查看截图:

Headers - grab.by/on5Q
Post - grab.by/on5U
Response - grab.by/on5W
Coockies - grab.by/on5Y

2 个答案:

答案 0 :(得分:5)

如果没有看到JSON响应,我必须猜测,问题在于,您使用的是JSON的错误(或没有)引号。您必须使用 double 引号(对于属性名称和字符串值),或者至少在遇到该问题时,双引号会解决它。

示例:

{"id":1,"name":"TOM"}

我希望这会有所帮助。

答案 1 :(得分:2)

对于出现此类错误的其他朋友,请尝试将JSON值返回到您发送值的网页。

就我而言,我在localhost:8080/sender页面上将JSON数据发送到localhost:8110/receiver页面。在接收者页面获取数据后,将{}之类的内容返回给发件人页面。否则,即使服务器返回200 HTTP代码,也会调用错误回调。