1

I've wrote a snippet for axis room API. While passing the json for testing, I have got Cross-Origin Request Blocked error.

At the same time console shows: {"message":"Could not parse the message.","status":"Error"}. (see the below image)

enter image description here

I hasn't solution for Cross-Origin Request Blocked error. But now I want to show the Could not parse the message error. How to do that?. Is a good way to solve Cross-Origin Request Blocked error. suggest me please.

My JavaScript code

<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>
Milad Rashidi
  • 1,296
  • 4
  • 22
  • 40
Sagotharan
  • 2,586
  • 16
  • 73
  • 117
  • Rule #1 of JSON: **Don't try to build JSON manually.** Your data parameter is invalid. (Just pass it an object and let jQuery build the object, i.e. remove the double quotes.) – JJJ May 30 '14 at 07:46
  • 1
    As for the CORS issue, see [How do I send a cross-domain POST request via JavaScript?](http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript). – JJJ May 30 '14 at 07:47
  • see my new edit sir, now i change a correct formatted json. As for the CORS issue, who want to code sir. me or axis company (who was created API)? – Sagotharan May 30 '14 at 08:16

2 Answers2

1

You cannot get the response status, code nor headers of the XHR object via javascript for a cross origin request... you just got a success status but empty response.

konghou
  • 557
  • 7
  • 20
1

First of all your json is not correct formated.

Go to jsonlint and paste your json in there. I guess you have to use double cotes like this.

test it, and come back if it does not work.

{
    "accessKey": "7eb228097576abf56968e9845ab51b90",
    "channelId": "103",
    "hotels": [
        {
            "hotelId": "2",
            "rooms": [
                {
                    "roomId": "1",
                    "availability": [
                        {
                            "date": "2014-05-30",
                            "free": 1
                        },
                        {
                            "date": "2014-05-31",
                            "free": 1
                        }
                    ]
                }
            ]
        }
    ]
}
Andreas Lyngstad
  • 4,887
  • 2
  • 36
  • 69