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"
this is my php function that return a invalid 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 :
[{"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
$(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);
}
});
});
});
CodePudding user response:
Consider the following.
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"
}
}]
CodePudding user response:
what output you get.. when you put console.log(response)
var parsedJson= $.parseJSON(response);
or
var parsedJson= JSON.parse(response);
if(parsedJson[0].status==1){
}