javascript - ajax return value problem

WBOY
Release: 2016-09-28 08:54:09
Original
867 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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...

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template