2

Problem:

Uncaught SyntaxError: Unexpected token <

What I tried

Request: Jsonp
Response: returned XML Response 200.
Cross Domain Request that's why used data type: jsonp

Script code:

$.ajax({
    url: "some url",
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    type: "POST", /* or type:"GET" or type:"PUT" */
    data:myusername,
    crossDomain: true, 
    dataType: 'jsonp',
    data: {
        'name':myusername
    },
    success: function (result) {
        console.log(result);
    },
    error: function () {
        console.log("error");
    }
    //});
});

Here AJAX response is XML.
But can someone tell how can I solved unexpected token problem.
Response is XML.

solution i found is third party request

 var urljson='cross-damin-url';


   var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + 
     encodeURIComponent('select * from xml where url="' + urljson + '"') + 
   '&format=xml&callback=?';


    $.getJSON(yql, function (data) {
      console.log(data.results[0]);
       });

Any suggestion is most welcome.

5
  • 3
    The response is XML, and you are trying to parse it as JSON. You expect that to work...why? Commented Oct 4, 2017 at 12:50
  • i m trying to parse xml not json Commented Oct 4, 2017 at 12:52
  • When you set the response data type to json or jsonp jquery will attempt to parse it as such for you. Which is failing with a parse error for reasons I hope are now obvious. Also note that jsonp is inherently insecure. If the cross domain resource doesn't have a CORS header, you may want to just pipe it through your own domain. Commented Oct 4, 2017 at 12:54
  • I'm voting to close this question as "unclear what you are asking". If you can edit it to be coherent I'll remove the vote. What I think you are asking is a combination of "How do I CORS ajax request?", "What do I do if a resource on another domain doesn't have a CORS header?", and "How do I parse an xml response from an ajax call?". Please ask a single, clear question. Commented Oct 4, 2017 at 13:07
  • can anyone suggest best way to get xml result with any error in cross domain request using js Commented Oct 4, 2017 at 14:07

2 Answers 2

2

the problem is the line "dataType: 'jsonp'". Setting it this way the ajax call expect the result to be a jsonp (a json object wrapped as a params in a callback...) this is useful if you're doing a cross domain call (due to CORS). You have to set a "dataType: 'xml'"

Edit considering the you have to do a CORS call, you have to change the output of the api, at least making something like

callbackName({value:'<your><xml>...</xml></your>'})

and parsing the xml from the string "callbackName" is the name of a function or method declared in the page which make the ajax call and which will parse the json from the api... for instance something like:

function callbackName(result) {
  console.log($.parseXML(result.value)); 
}
Sign up to request clarification or add additional context in comments.

8 Comments

my requirement cros "dataType: 'xml' its stops when cros request
well, then you have to change the output of the api, at least making something like "callbackName({value:'... you xml ...'}) and parsing the xml from the string P.s. "callbackName" is the name of a function or method declared in the page which make the ajax call and which will parse the json from the api... for instance something like function callbackName(result) { console.log(result.value); }
can you give some example im new to jsonp
@RobertoBisello I gave you a +1, but please note that jsonp is inherently insecure and recommending its use is... arguable.
@JaredSmith I agree, jsonp is not really secure, but I've not recommended the use of jsonp, I've found a solution for afeef's problem considering the given context... honestly I dont's see any other solution :(
|
0

JSON and XML are arbitrarily nested data-interchange formats. You take a (well-formed) string and convert it to a data structure based on the rules of the format. Their similarities stop there.

So you can't just parse XML as JSON, they have different rules for turning a string (like that returned by an HTTP request) into a data structure. Which is why you're getting an error.

Parsing XML with an XML parser (like the one built into every browser) instead of a JSON parser will not throw an error.

But your actual problem is that it's a cross-domain resource and the browser (for good reason) won't let you. If you need a cross-domain resource and you don't control it and it doesn't have a CORS header you have a few options:

  1. Beg the entity that controls the resource to add a CORS Header
  2. Pipe the resource through your domain.

To expand on option 2:

Your page requests the resource from your webserver, your webserver makes an http request to the cross-origin resource, and then sends the result back to the browser.

About JSONP

JSONP was a hack invented before CORS to bypass the browser's security model. It was (meant) to be used by people who understood the risks, knew what they were doing and why. It involves potentially executing arbitrary third-party code on your page. Needless to say, that's bad.

2 Comments

hi jared does we need third party url (if server has not allowed cross domain true.)
@afeef then you'll have to do option 2 and fetch the data server-side and return it to the browser so it comes from your domain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.