2

I am accessing a cross domain api using jquery ajax but I can't achieve it . Iam getting error as "uncaught syntaxerror : unexpected token <" in the console, How to fix this.

Code:

$.ajax({ 
url: "http://..............", 
type:"GET", 
dataType: 'jsonp', 
crossDomain: true, 
contentType:"application/javascript", 
success: function (data) { 
    alert(data);
}, 
error: function (errorMEssage, Errtext) { 
    alert(Errtext);
} 
});

Error:

uncaught syntaxerror : unexpected token <
Vignesh
  • 1,458
  • 12
  • 31
  • 59

2 Answers2

3

You cannot make cross domain AJAX calls using JSONP to a server that returns XML. If you want to be able to make a cross domain AJAX call you have 2 possibilities:

  • use JSONP -> your server needs to support it.
  • use CORS -> your server AND client browser need t support it.

If your server supports CORS your request may look like this:

$.ajax({ 
    url: "http://..............", 
    type:"GET", 
    crossDomain: true, 
    success: function (data) { 
        alert(data);
    }, 
    error: function (errorMEssage, Errtext) { 
        alert(Errtext);
    } 
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Use JSONP or CORS. You will need to modify the remote endpoint to support one of the 2. Only then you will be able to make cross domain AJAX calls. – Darin Dimitrov Oct 17 '13 at 07:29
  • NO. CORS means Cross Origin Resource Sharing. Please read the article I have linked to in my answer. Your server side script will need to send the `Access-Control-Allow-Origin` response headers to an OPTIONS verb request. Once you ensure that the remote server sends this header you could remove the `dataType: 'jsonp'` parameter from your AJAX call. – Darin Dimitrov Oct 17 '13 at 07:31
  • but the thing is iam getting 200 success and in console if i click that error i can able able to view xml response – Vignesh Oct 17 '13 at 07:32
  • Then maybe the server already supports CORS. Try removing the `dataType: 'jsonp'` parameter. Also change the `contentType` parameter. – Darin Dimitrov Oct 17 '13 at 07:33
  • don't specify dataType parameter, remove that is that you are saying – Vignesh Oct 17 '13 at 07:36
0

change the dataType to xml

dataType: 'xml', 
Praveen
  • 55,303
  • 33
  • 133
  • 164