Ajax and JSON in the front and back of the details of the doubts

Source: Internet
Author: User

Ajax Request JSON

Thinkphp in the TP3.2 development Manual of whether or not to judge Ajax, there is a paragraph: " note that if you are using Thinkajax or your own Ajax class library, you need to add a hidden field in the form, tell the background is the Ajax way to commit, The default hidden domain name is AJAX (which can be configured via Var_ajax_submit) and, if it is a jquery class library, it can be automatically judged without adding any hidden fields. "

I haven't even looked at its source code, and it's here.

Define (' Is_ajax ',   (strtolower($_server[' http_x_requested_with ']) = = ' XMLHttpRequest '
|| ! Empty ($_post
|| ! Empty ($_gettruefalse);

It is obvious that the following verification of post and get is the validation of the submitted hidden domain that corresponds to the Ajax you wrote. So that means that the $_server[' Http_x_requested_with ' is judged by the AJAX submission of Jquery, and then using Firebug to view an AJAX request, you can see

When you see the last line in the request header information, there is a x-requested-with value XMLHttpRequest。这个就是jQuery的ajax方法中生成的,当然我们自己写的ajax提交方法也可以这样写,详情可以看这篇文章。我这里点到为止,了解了原理即可。 of

Ajax returns the JSON format service side

Original wording

$data Array (' Code ' = ' 0 ', ' msg ' = ' modified successfully '); Echo $data );

The result of this write-front is a JSON string that needs to be converted to a JSON object using the Json.parse () method, taking care not to use the eval () method. In addition, the low version of IE may not natively support JSON, to reference the Json2.js function library.

Framework notation

Some frameworks provide encapsulated Ajax JSON format return methods, such as thinkphp provides Ajaxreturn ($data). The result of this method is a JSON object, do not need to convert, JS can be used directly.

Improve the original wording

Why can a method in a frame return a JSON object? This is related to HTTP header knowledge. Header status type has 404,200,403, header content type has HTML, pictures, videos and so on. The setting of the JSON object in the header is a simple way to set the header in the content type json,php. Add the header (' Content-type:text/json ') before Echo;

Array (' Code ' = ' 0 ', ' msg ' = ' modify Success ');
Header (' Content-type:text/json ');
$DATA);

Similarly, when generating a picture using the GD library (such as a verification code), you also need to use the header function to set the type as an image, such as the header ("content-type:image/png;");

Ajax returns the JSON format browser side

$.get (); and $.post (); is the original data returned by the server, such as the server returns a string is a string, the return is JSON object is JSON object;

$.getjson (); The resulting JSON object or error, regardless of whether the server returns a string or a JSON object. If the data returned by the server does not conform to the JSON specification, the error will be.

Ajax and JSON in the front and back of the details of the doubts

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.