0

Calling a webservice written in php and ajax call is ending up in error function

 <script>
            $(document).ready(function(){
                alert('ajax');
                $.ajax({ 
                               type: "GET",
                    dataType: "json",
                   // crossDomain: true,
                     contentType: "application/json",
                    url: "http://domain/Customer/getCountryList",
                    headers: {
                        Accept: "application/json",
                        "Access-Control-Allow-Origin": "*",
                       "Access-Control-Allow-Methods": "GET"
                   },
                   success: function(data){   
                       alert("In S"); 
                    },
                    error:function(xhr,statusText){
                    alert("In N");

                  alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
                   }
                })
            })

        </script>

And teh service file output is

[{"responseCode":"0"},
[{"countryId":"1","countryName":"USA","isdCode":"","isActive":"\u0001"}]] 

how to parse this out put

AVM
  • 592
  • 2
  • 11
  • 25

2 Answers2

0

First: this is response header not request header, you can send request header only from $.ajax()

headers: {
       Accept: "application/json",
       "Access-Control-Allow-Origin": "*",
       "Access-Control-Allow-Methods": "GET"
}

Parse error:

You are trying to access other domain using ajax but while web server won't provide above header until you can't access response using AJAX, this is called Cross-Origin Request Blocked

Reason Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at web server. This can be fixed by moving the resource to the same domain or enabling CORS. http://www.w3.org/TR/cors/

You can also access another server response if server provide JSONP callback method, see more detail click here

Community
  • 1
  • 1
Girish
  • 11,907
  • 3
  • 34
  • 51
  • "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET" specified the above two lines in the service file also CORS is enabled,but still the same error is showing up – user4022479 Dec 26 '14 at 07:13
  • then no need to pass these header in ajax request header these response headers – Girish Dec 26 '14 at 07:15
  • so your issue on response `error: function()` calling? – Girish Dec 26 '14 at 07:17
0

If everything else is working fine
dataType: "json" means you parse incoming data to json.

However you json data is invalid due to "\u0001" string.

So remove dataType: "json" and replace

data = data.replace("\u0001", "\\\\u0001")

and then parse to json.

DEMO

asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84