[英]I am using ionic and getting an error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)
[英]While fetching JSON data using ajax i am getting Unexpected token : error
我需要从url获取数据返回的数据是JSON。但是在尝试获取时我正在获取
未捕获的SyntaxError:意外的令牌:
这是代码请检查。
你能告诉我为什么我会收到这个错误以及如何解决它。
$(document).ready(function () { var Url = "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/ajaxGetQuoteJSON.jsp?symbol=LIBERTSHOE"; $.ajax({ contentType: 'application/json', dataType: 'json', url: Url + '&callback=?', success: function (data) { alert(data); }, error: function (jqXHR, text, errorThrown) { } });});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
看来你无法在JSON中构建URL字符串
而不是像这样在JSON中创建它:
url: Url + '&callback=?',
您可以将其添加到原始URL字符串的末尾:
var Url = "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/ajaxGetQuoteJSON.jsp?symbol=LIBERTSHOE&callback=?'";
http://codepen.io/nilestanner/pen/pEOgZj
尽管codepen仍显示交叉原点错误,但这会删除语法错误。
我希望这能解决这个问题。
$(document).ready(function() { var Url = "https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/ajaxGetQuoteJSON.jsp?symbol=LIBERTSHOE&callback=?"; $.ajax({ contentType: 'application/json', dataType: 'json', url: Url }).then(function(data){ //What should happen in success scenarios console.log('Success'); alert(data) },function(data){ //what should happen in failure scenarios console.log('Failure Block'); alert(data) }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.