Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

javascript - Unexpected token < reading xml file

I'm having a problem when i do an ajax call.

My code is :

$('#regLink').click(function(event){
  event.preventDefault();
  urlLink = $('#regLink').attr('name') + $('input[name="email"]').val();
  $.ajax({
    type: 'GET',
    dataType: 'jsonp',
    jsonpCallback: 'callback',
    url: urlLink,
    success: function (xml) {
      var code = $(xml).find('int').text();
      if(code == '1') {
        console.log('email gravado com sucesso');
      }
      else {
        console.log('algo correu mal');
      }
    }
  });
});

The call give this response :

<?xml version="1.0" encoding="utf-8"?> 
<int xmlns="http://tempuri.org/">1</int>

And it give me this error :

Uncaught SyntaxError: Unexpected token <

The URL points to another site, so its a cross domain call.

I just want to read the "1" and give a success alert.

Hope you can help me.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your JavaScript says to make an Ajax request using the JSONP pattern (which involves inserting a <script src="something"> element into the document instead of using XMLHttpRequest).

The response you are getting is an XML document, not a JavaScript program following the JSONP pattern, which would be something like:

value_of_query_string_callback_argument({ "int": 1 });

You get the error because the browser is trying to execute the XML as JavaScript (which it isn't).

Either change the response to be JSONP, or use some other method to make a cross-domain request.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...