最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery AJAX POST call return 200 status OK but error - Stack Overflow

programmeradmin4浏览0评论

Hi I have implemented one Ajax POST request to call web service. It always return 200 OK but execute failed event I have tried lot of things but I am not getting where I am doing mistake. I am adding my code here. Fiddler shows the response content but Ajax is not executing success event.

<!DOCTYPE html>
<html><head>
<script src=".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>

I have tried dataType:'text'....response from server is in proper JSON format

Request JSON data :

{
    "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"
}

Response JSON data:

{
    "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 error: {"readyState":0,"responseText":"","status":0,"statusText":"error"}

I am adding screenshots which is taken from firebug please go to tinygrab url to see screenshot:

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

Hi I have implemented one Ajax POST request to call web service. It always return 200 OK but execute failed event I have tried lot of things but I am not getting where I am doing mistake. I am adding my code here. Fiddler shows the response content but Ajax is not executing success event.

<!DOCTYPE html>
<html><head>
<script src="http://code.jquery./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>

I have tried dataType:'text'....response from server is in proper JSON format

Request JSON data :

{
    "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"
}

Response JSON data:

{
    "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 error: {"readyState":0,"responseText":"","status":0,"statusText":"error"}

I am adding screenshots which is taken from firebug please go to tinygrab url to see screenshot:

Headers - grab.by/on5Q
Post - grab.by/on5U
Response - grab.by/on5W
Coockies - grab.by/on5Y
Share Improve this question edited Jul 11, 2013 at 5:24 Nitesh Virani asked Jul 10, 2013 at 16:17 Nitesh ViraniNitesh Virani 1,7124 gold badges26 silver badges43 bronze badges 7
  • 1 Is your webservice on the same domain as this page? If not, you need to use dataType: 'jsonp' in order to avoid violating the Same Origin Policy. – Lexi Tramel Commented Jul 10, 2013 at 16:21
  • Could you Post the JSON Result? Sometimes the Problem lies on the Format na dthe differnt types of quotes. – winner_joiner Commented Jul 10, 2013 at 16:27
  • No web service is on the other domain. I cannot use 'jsonp' since web service only support POST type request. – Nitesh Virani Commented Jul 11, 2013 at 3:59
  • 1 possible duplicate of jQuery ajax function returns an error – winner_joiner Commented Jul 11, 2013 at 6:09
  • 1 Have you found already an solution? If so please share. THX :) – winner_joiner Commented Jul 16, 2013 at 6:33
 |  Show 2 more ments

2 Answers 2

Reset to default 6

Without seeing the JSON Response i would have to guess, that the Problem is, that you are using the Wrong(or no) Quotes for the JSON. You must use double Quotes(for property names and string values), or at least when i had that problem, the double quotes solved it.

Example:

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

i hope this helps.

For other friends that have such an error, try to return a JSON value to the page that you send your values from.

In my case, I'm on localhost:8080/sender page that sends JSON data to localhost:8110/receiver page. After receiver page gets the data, return something like {} back to the sender page. Otherwise, the error callback is called even if the server returns a 200 HTTP code.

发布评论

评论列表(0)

  1. 暂无评论