V
V
valikv1per2014-04-15 00:09:25
PHP
valikv1per, 2014-04-15 00:09:25

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;

                }
}

Server script
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

7 answer(s)
B
bahek2462774, 2014-04-15
@bahek2462774

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);
    });

B
bahek2462774, 2014-04-15
@bahek2462774

what does console.log(data) show?

D
Daniel, 2014-04-15
@ZOTTCE

Are the server address and the address of the site from which the request is made the same?
If not, then use JSON-P

S
Sergey Melnikov, 2014-04-15
@mlnkv

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);
});

See what will be output to the console

A
Alexey, 2014-04-15
@rdifb0

You obviously have extra non-printable characters there. Look at the variable in the debugger, maybe it will be more visible.

N
Nikita Baev, 2014-04-15
@drugoi

And what dataType is sent by the server?

D
Daniel, 2014-04-15
@ZOTTCE

Fix encoding issues. The problem of erroneous data parsing is precisely in this.
And the browser also finds errors in the HTML code of the page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question