Explore some small problems in Ajax _ajax related

Source: Internet
Author: User
Tags php json script tag string format

1.ajax cross-domain pass value is the type of postback required JSONP

$.ajax ({
URL: "http://...",
type: ' Get ',
dataType: ' JSONP ',//js cross-domain value
success:function (data) {
}
});

DataType

Type: String

The type of data expected to be returned by the server. If not specified, JQuery will automatically intelligently judge against HTTP packet mime information, such as XML MIME types being recognized as XML. In 1.4, JSON generates a JavaScript object, and script executes it. The data returned by the server side will then be based on

After this value is parsed, it is passed to the callback function. Available values:

"XML": Returns an XML document that can be processed with jQuery.

HTML: Returns plain text HTML information, and the included script tag executes when the DOM is inserted.

Script: Returns the plain text JavaScript code. Results are not automatically cached. Unless the "cache" parameter is set. Note: On remote requests (not in the same domain), all POST requests are converted to get requests. (because it will be loaded using the DOM's script tag)

"JSON": Returns JSON data.

"JSONP": Jsonp format. When calling a function using JSONP form, such as "myurl?callback=?" JQuery is automatically replaced? To the correct function name to execute the callback function.

2.ajax Demo sample with PHP to get the value

$ (function () {
var my_data= "foreground variable";
My_data=escape (My_data) + "";//encode to prevent Chinese characters from garbled
$.ajax ({
URL: "ajax_php.php",
Type: "POST",
data:{trans_ Data:my_data},
//datatype: "JSON",
error:function () {
alert (' Error loading XML document ');
success:function (data,status) {//If invoking PHP Success
alert (unescape (data);//decoding, displaying kanji
}
}
);

PHP code

Copy Code code as follows:

$backValue =$_post[' Trans_data ']; echo $backValue. " + Backstage Return ";

Data conversion processing for 3.php JSON

Json_decode (String $json [, bool $assoc]); Accepts a JSON-formatted string and converts it to a PHP variable
Json_decode ($data, true);

JSON string format to be decoded by JSON. Assoc When this argument is TRUE, the array is returned instead of object.

Json_encode (mixed $value [, int $options = 0]) returns the JSON form of value values
Json_encode ($a)

The above probably to share three Ajax small problems, very common, hope to help everyone!

Related Article

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.