获取ajax中的响应参数值

时间:2014-05-30 07:37:11

标签: javascript jquery ajax json

我已经为轴室API编写了一个片段。在通过json进行测试时,我有跨源请求被阻止错误。

同时控制台显示:{"message":"Could not parse the message.","status":"Error"}。 (见下图)

enter image description here

我没有针对跨源请求阻止错误的解决方案。但现在我想显示无法解析消息错误。怎么做?。是解决跨源请求阻止错误的好方法。建议我。

我的JavaScript代码

<script type="text/javascript">
    $(function () {
        $('#Button1').click(function () {
            alert('Alter with jQuery Button Clicked');
            alert('Clicked');
            $.ajax({
                type: 'POST',
                url: 'http://test.axisrooms.com/api/daywiseInventory',
                data: '{"accessKey": "7eb228097576abf56968e9845ab51b90","channelId": "103","hotels": [{"hotelId": "2","rooms": [ {"roomId": "1", "availability": [ { "date": "2014-05-30","free": 1},{"date": "2014-05-31","free": 1}]}]}]}',
                //data: "{accessKey':'cilentAPIKey'}",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (data) {
                    callback(data);
                    alert('sus'+data.toString());
                },
                error: function (response) {
                    //alert(response);
                    //alert('error' + response.valueOf(message));                    
                    alert('An error has occured');
                }

            });
        });
    });
</script>

2 个答案:

答案 0 :(得分:1)

您无法通过javascript获取XHR对象的响应状态,代码或标题以获取跨源请求...您刚刚获得成功状态但响应为空。

答案 1 :(得分:1)

首先,你的json格式不正确。

转到jsonlint并将您的json粘贴到那里。我想你必须使用像这样的双骰子。

测试它,如果不起作用则返回。

{
    "accessKey": "7eb228097576abf56968e9845ab51b90",
    "channelId": "103",
    "hotels": [
        {
            "hotelId": "2",
            "rooms": [
                {
                    "roomId": "1",
                    "availability": [
                        {
                            "date": "2014-05-30",
                            "free": 1
                        },
                        {
                            "date": "2014-05-31",
                            "free": 1
                        }
                    ]
                }
            ]
        }
    ]
}