Home  >  Article  >  Backend Development  >  javascript - ajax return value problem

javascript - ajax return value problem

WBOY
WBOYOriginal
2016-09-28 08:54:09836browse

ajax request code is as follows:
javascript - ajax return value problem
php processing part:

javascript - ajax return value problem
firebug:

javascript - ajax return value problem
Why can’t I print it? ? ?

Reply content:

ajax request code is as follows:
javascript - ajax return value problem
php processing part:

javascript - ajax return value problem
firebug:

javascript - ajax return value problem
Why can’t I print it? ? ?

I prefer to use $.post(settings) directly (in fact, I prefer to use $.ajax(url, settings) directly), so that the parameters are clearer. The callback is set through done() of the promise returned by $.post() or $.ajax(), so that the structure is clearer

<code class="javascript">$.post({
    url: theurl,
    dataType: "json",
    data: {
        tablename: _tbname
    }
}).done(function(jo) {
    // jo 是 javascript object
    // 从后端返回的 JSON 解析而来
    console.log(jo);
});</code>

Using your method, you can also add a parameter "json" directly after the callback to indicate the data type

<code>$.post("http://url/", { tablename: _tbname}, function(data) {}, "json");</code>

json_encode($data,JSON_UNESCAPED_UNICODE)

No error is reported, please restart firefox and have a look
PS: Set the response header @header("Content-Type:application/json;charset=utf8");
You can omit the step of var json = eval(data);
Moreover, it is not safe to use eval, it is recommended to use

<code>var json = jQuery.parseJSON(data);</code>

Change Chrome (I’m really not used to Firefox debugging, forget it if you get used to it), break point debugging, there is probably no way to enter. I haven’t used Firefox for a long time, and I vaguely remember that I had a similar problem before. This should report an error under Chrome, but not under Firefox. Please interrupt first and take a look. If you can't enter the method, try changing to chrome.

The data returned by default is a string, not a json object. The first solution is to specify the return type as json in the request parameter, and jquery will parse it into an object internally. The second solution is to deserialize the json string into an object yourself. For example: http://blog.163.com/m13864039...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn