Javascript-ajax return value problem

Source: Internet
Author: User

The AJAX request code is as follows:

PHP Processing Section:


Firefox Firebug:


Can't print out what's going on???

Reply content:

The AJAX request code is as follows:

PHP Processing Section:


Firefox Firebug:


Can't print out what's going on???

I prefer to use directly $.post(settings) (in fact, prefer $.ajax(url, settings) direct), so that the parameters are more clear. The callback $.post() is $.ajax() set through or returned by the Promise done() , so the structure is relatively clear

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

In your way, you can also add a parameter "JSON" directly behind the callback to represent the data type

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

Json_encode ($data, Json_unescaped_unicode)

Even if there is no error, you restart Firefox to see it
PS: Set the response header @header ("Content-type:application/json;charset=utf8");
You can omit var json = eval (data); This step.
Also, using eval is unsafe, it is recommended to use

var json = jQuery.parseJSON(data);

Change Chrome (I am really not accustomed to Firefox debugging, you are accustomed to that even if), interrupt point debugging, mostly do not enter the method. Long time no use of Firefox, vaguely remember the past seems to have had similar problems, this in chrome should be an error, in Firefox without error. Just break it up and try Chrome if you don't get into the way.

The default returned data is a string, not a JSON object, one solution, the request parameter specifies that the return type is json,jquery internally resolves to an object, solution two, self-speaking JSON string deserialized into an object. For example: http://blog.163.com/m13864039 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.