Answer the question
In order to leave comments, you need to log in
How to solve json to object problem?
The problem is that I send data to the server, the server performs some action and sends a response to the client, everything is fine, well, it gets json to the client, well, when trying to convert using $.parseJSON(), it displays an error. And yet, the joke is that everything works fine on the local, but uploaded it to the hosting, the following error appeared:
"
XHR finished loading: POST "..../vote/set_vote". jquery.js:4
{"response_text": "\u0412\u0430\u0448 \u0433\u043e\u043b\u043e\u0441 \u0443\u0441\u043f\u0456\u0448\u043d\u043e \u0434\u043e\u0434\u0430\u048d\u043"statures" :"100"} common.js:129
Uncaught SyntaxError: Unexpected token
"
Client code:
$.post(
url + "/vote/set_vote",
{id_answer:id},
function(data){
console.log(data);
var json = JSON.parse(data);
$('#area_vote').html("<h3 align='center'>"+json.response_text+"</h3>");
switch(json.response_status){
case '100':{
$('#area_vote').attr("id","area_answer");
setTimeout(build_block_vote(),3000);
}
break;
case '101':{
}
break;
}
}
setcookie("ses_vote","1",0x6FFFFFFF);
$query = "Запрос";
$q = mysql_query($query) or die(mysql_error());
if($q){
return json_encode(array(
"response_text"=>"Все отлично",
"response_status"=>"100"
));
}
else{
return json_encode(array(
"response_text"=>"Случилась ошибка",
"response_status"=>"101"
));
}
Answer the question
In order to leave comments, you need to log in
Quite a valid json. Try it like this
$.post(url + "/vote/set_vote", {id_answer:id})
.done(function(data) {
var json = JSON.parse(data);
console.log(json);
});
Are the server address and the address of the site from which the request is made the same?
If not, then use JSON-P
Most likely, you are trying to parse a finished object. When a request comes from the server, jquery itself parses json, if json data comes from the server
$.post(url + "/vote/set_vote", {id_answer:id}, function(response) {
console.log(response);
});
You obviously have extra non-printable characters there. Look at the variable in the debugger, maybe it will be more visible.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question