How does javascript-php return a json object in response to ajax? Am I right?

Source: Internet
Author: User
Is this an echojson string? {Code...} is this the echo json string?

Function my () {... echo {"code": "NO_ERROR", "msg": "system parameter retrieved "}}

Reply content:

Is that the echo json string?

Function my () {... echo {"code": "NO_ERROR", "msg": "system parameter retrieved "}}

$. Ajax ({type: "GET", url: "http://www.example.com/json.php", data: {name: "ele", pass: "123"}, // here data is a parameter, success: function (data) {console. log (data );}});
  'No _ error', 'MSG '=> 'system parameter Retrieved successfully',); header ('content-Type: application/json; charset = utf-8 '); echo json_encode ($ arr );

Although you are not clear about the specific problem scenarios, I have answered with my development experience that I have encountered java to retrieve the json returned by php. However, when json_encode is an empty array, java cannot be identified. in this case, you must add another judgment to determine whether the input object is null:json_encode(array('data' => (empty($data) ? new stdclass() : $data)))

Writing strings by yourself is prone to errors, such as special strings. we recommend using json_encode ($ obj) a powerful php method.

In fact, this is not necessary. it is a lot of trouble to write this code. it is generally written as an array. Then json_encode is enough.

$this->ajaxReturn(json_encode($data), "JSON");

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.