Discuss Some small issues in Ajax and discuss Ajax issues

Source: Internet
Author: User

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!

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.