简体   繁体   English

get data from php simple html dom into json but can't pasre this json in javascript

[英]get data from php simple html dom into json but can't pasre this json in javascript

I am trying to create a json data from php simple html dom parser But it almost returns invalid data because I'm trying to parse this json response in ajax request and i get this error in console "parsererror SyntaxError: Unexpected non-whitespace character after JSON at position 913" I am trying to create a json data from php simple html dom parser But it almost returns invalid data because I'm trying to parse this json response in ajax request and i get this error in console "parsererror SyntaxError: Unexpected non-whitespace character after JSON 在 position 913"

this is my php function that return a invalid json这是我的 php function 返回无效的 json

function getUrls($url){
    $responses= array();
    $response2["status"] = 1;
    $responses[] = $response2;
    $fbPost = grab_page($url);
    $postUsers = new simple_html_dom();
    $postUsers->load($fbPost);
    foreach($postUsers->find('#m_story_permalink_view h3 a') as $fbUserDiv) {
        $data = $fbUserDiv->href;
        $userurl['user_url'] = $data;
        $response["data"] = $userurl;
         
    
        $responses[] = $response;
    }
    echo json_encode($responses);

}

And this is my json response:这是我的 json 回复:

[{"status":1},{"data":{"user_url":"\/profile.php?id=100006046927552&refid=18&__tn__=C-R"}},{"data":{"user_url":"https:\/\/mbasic.facebook.com\/groups\/ultrasfci2\/?refid=18&__tn__=C-R"}},{"data":{"user_url":"\/profile.php?id=100071323021139&refid=18&__tn__=R"}},{"data":{"user_url":"\/ammar.hosny.33?refid=18&__tn__=R"}},{"data":{"user_url":"\/rawan.magdy.37017?refid=18&__tn__=R"}},{"data":{"user_url":"\/michaelabdo.michaelabdo.9?refid=18&__tn__=R"}},{"data":{"user_url":"\/profile.php?id=100022618071315&refid=18&__tn__=R"}},{"data":{"user_url":"\/abrar.agour?refid=18&__tn__=R"}},{"data":{"user_url":"\/ahmedaymendaana?refid=18&__tn__=R"}},{"data":{"user_url":"\/ahmed.abdulrhman.9231712?refid=18&__tn__=R"}},{"data":{"user_url":"\/profile.php?id=100024243560746&refid=18&__tn__=R"}},{"data":{"user_url":"\/abdallh.gmal.393?refid=18&__tn__=R"}}]

my Java Script code我的 Java 脚本代码

$(document).ready(function(e){

    $("#getUsersId").on('submit', function(e){
    e.preventDefault();

     $.ajax({
            type: "post",
            url: "fbReq.php",
            data: new FormData(this),
            dataType: 'json',
            contentType: false,
            cache: false,
            processData:false,
            success: function (response) {

                if(response.status == 1){

                    console.log("success");
                    $.each(response, function(key, value) { 

                        $("#myTextarea").val(value.data.user_url);
     
                    });
            }

            },
            error: function(jqXHR, textStatus, errorThrown) {
               console.log(textStatus, errorThrown);
            }
        });

        });



});

Consider the following.考虑以下。

JavaScript JavaScript

$("#getUsersId").on('submit', function(e) {
  e.preventDefault();
  $.ajax({
    type: "post",
    url: "fbReq.php",
    data: new FormData(this),
    dataType: 'json',
    contentType: false,
    cache: false,
    processData: false,
    success: function(response) {
      if (response[0].status == 1) {
        console.log("success");
        $.each(response, function(i, val) {
          if(i > 0){
            $("#myTextarea").val(val.data.user_url);
          }
        });
      }
    },
    error: function(jqXHR, textStatus, errorThrown) {
      console.log(textStatus, errorThrown);
    }
  });
});

This properly addresses a response like:这正确地解决了如下响应:

[{
  "status": 1
}, {
  "data": {
    "user_url": "\/profile.php?id=100006046927552&refid=18&__tn__=C-R"
  }
}, {
  "data": {
    "user_url": "https:\/\/mbasic.facebook.com\/groups\/ultrasfci2\/?refid=18&__tn__=C-R"
  }
}, {
  "data": {
    "user_url": "\/profile.php?id=100071323021139&refid=18&__tn__=R"
  }
}, {
  "data": {
    "user_url": "\/ammar.hosny.33?refid=18&__tn__=R"
  }
}, {
  "data": {
    "user_url": "\/rawan.magdy.37017?refid=18&__tn__=R"
  }
}, {
  "data": {
    "user_url": "\/michaelabdo.michaelabdo.9?refid=18&__tn__=R"
  }
}, {
  "data": {
    "user_url": "\/profile.php?id=100022618071315&refid=18&__tn__=R"
  }
}, {
  "data": {
    "user_url": "\/abrar.agour?refid=18&__tn__=R"
  }
}, {
  "data": {
    "user_url": "\/ahmedaymendaana?refid=18&__tn__=R"
  }
}, {
  "data": {
    "user_url": "\/ahmed.abdulrhman.9231712?refid=18&__tn__=R"
  }
}, {
  "data": {
    "user_url": "\/profile.php?id=100024243560746&refid=18&__tn__=R"
  }
}, {
  "data": {
    "user_url": "\/abdallh.gmal.393?refid=18&__tn__=R"
  }
}]

what output you get.. when you put console.log(response)你得到什么 output .. 当你把console.log(response)

var parsedJson= $.parseJSON(response);

or或者

var parsedJson= JSON.parse(response);
if(parsedJson[0].status==1){
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM