最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot use 'in' operator to search for 'length' in - Stack Overflow

programmeradmin1浏览0评论

I read this answer

And I did this:

function countryPage() {
  $.ajax({
    url: ".php?action=parse&disablelimitreport=true&format=json&prop=text|langlinks&noimages=true&mobileformat=true&page="+ curTitle + "&callback=?",
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    success: countryPageSuccess
  });
}

function countryPageSuccess(counterObject, data) {
  $.each(data, function(i, item) {...

But if I then do as per that answer

 $.each(JSON.parse(data), function(i, item) {

I get

Uncaught SyntaxError: Unexpected token o in JSON at position 1

I read this answer

And I did this:

function countryPage() {
  $.ajax({
    url: "https://en.wikipedia/w/api.php?action=parse&disablelimitreport=true&format=json&prop=text|langlinks&noimages=true&mobileformat=true&page="+ curTitle + "&callback=?",
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    success: countryPageSuccess
  });
}

function countryPageSuccess(counterObject, data) {
  $.each(data, function(i, item) {...

But if I then do as per that answer

 $.each(JSON.parse(data), function(i, item) {

I get

Uncaught SyntaxError: Unexpected token o in JSON at position 1

Share Improve this question edited Aug 3, 2017 at 2:39 rob.m asked Aug 3, 2017 at 2:30 rob.mrob.m 10.6k21 gold badges88 silver badges175 bronze badges 13
  • Log data and take a look at what it contains, more than likely its already an object – Patrick Evans Commented Aug 3, 2017 at 2:34
  • @PatrickEvans ok but if I don't do JSON.parse I get the error in the question title and yes I confirm it is already an object – rob.m Commented Aug 3, 2017 at 2:35
  • The 2nd argument to the success callback is the textStatus. it won't be JSON. The JSON response should already be deserialized in the counterObject – Phil Commented Aug 3, 2017 at 2:36
  • what do you mean is the textStatus? @Phil – rob.m Commented Aug 3, 2017 at 2:37
  • 1 The data you are getting from the API is the first argument not the second (which you called data). Look at the jQuery ajax reference to see how the success callback is defined – Patrick Evans Commented Aug 3, 2017 at 2:39
 |  Show 8 more ments

1 Answer 1

Reset to default 2

It's already a JSON object. You cannot parse it again.

to receive HTTP error code, provide a error callback function next to success, showed my code below.

function countryPage() {
  $.ajax({
    url: "https://en.wikipedia/w/api.php?action=parse&disablelimitreport=true&format=json&prop=text|langlinks&noimages=true&mobileformat=true&page=1&callback=?",
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    success: countryPageSuccess,
    error: function(jqXHR, textStatus, errorThrown) {
        alert(jqXHR.status);
        alert(textStatus);
        alert(errorThrown);
    }
  });
}

function countryPageSuccess(data, result) {
    $.each(data, function(i, item) {
        console.log(item);
    });
}

countryPage();
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论