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

I read this answerAnd I did this:function countryPage() {$.ajax({url: ".php?action=parse&disab

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745320570a4622425.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信