Discuss Some small issues in Ajax and discuss Ajax issues
1. The cross-origin transfer value of ajax is the type of the required callback. It is jsonp.
$. Ajax ({url: "http ://....... ", type: 'get', ype: 'jsonp', // js cross-origin transfer value success: function (data ){}});
DataType
Type: String
Expected data type returned by the server. If this parameter is not specified, jQuery automatically determines Based on the MIME information of the HTTP package. For example, the xml mime type is recognized as XML. In 1.4, JSON will generate a JavaScript Object, and script will execute this script. Then the data returned by the server will
After this value is parsed, it is passed to the callback function. Available value:
"Xml": returns an XML document, which can be processed by jQuery.
"Html": returns plain text HTML information. The script tag is executed when the dom is inserted.
"Script": returns plain text JavaScript code. Results are not automatically cached. Unless the "cache" parameter is set. Note: During remote requests (not in the same domain), all POST requests are converted to GET requests. (Because the script tag of DOM will be used for loading)
"Json": Return JSON data.
"Jsonp": JSONP format. When calling a function in the form of JSONP, such as "myurl? Callback =? "Will jQuery be replaced automatically? For the correct function name to execute the callback function.
2. a demo of ajax can be used with php to obtain the value.
$ (Function () {var my_data = "foreground variable"; my_data = escape (my_data) + "; // encoding to prevent Chinese Character garbled characters $. 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 php is successfully called alert (unescape (data); // decode, show Chinese characters }});});
Php code
Copy codeThe Code is as follows:
$ BackValue = $ _ POST ['Trans _ data']; echo $ backValue. "+ background return ";
3. json Data Conversion in php
Json_decode (string $ json [, bool $ assoc]); // receives a JSON string and converts it to a PHP variable.
Json_decode ($ data, true );
Json string to be decoded. When this parameter is set to TRUE, assoc returns an array instead of an object.
Json_encode (mixed $ value [, int $ options = 0]) returns the JSON format of the value.
Json_encode ($)
I have shared with you about three small ajax problems. They are very common and hope to help you!