Home  >  Article  >  Web Front-end  >  Discussion and research on Ajax

Discussion and research on Ajax

php中世界最好的语言
php中世界最好的语言Original
2018-04-04 15:36:061383browse

This time I will bring you the discussion and research on Ajax. What are the precautions on the discussion and research on Ajax? The following is a practical case, let’s take a look.

1. Ajax cross-domain transfer value is required to return the type jsonp

$.ajax({
url: "http://.......",
type: 'GET',
dataType: 'JSONP',//js跨域传值
success: function (data) {
}
});

dataType

type : String

The data type expected to be returned by the server. If not specified, jQuery will automatically make intelligent judgments 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 the script. Then the data returned by the server will be parsed according to the

value and passed to the callback function . Available values:

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

"html": Returns plain text HTML information; the included script tag will be executed when inserted into the dom.

"script": Returns plain text JavaScript code. Results are not cached automatically. Unless the "cache" parameter is set. Note: When making remote requests (not under the same domain), all POST requests will be converted into GET requests. (Because the DOM script tag will be used to load)

"json": Returns JSON data.

"jsonp": JSONP format. When calling a function using JSONP form, such as "myurl?callback=?" jQuery will automatically replace ? with the correct function name to execute the callback function.

2. A demo sample of ajax with php to obtain the json data of the value

$(function(){
var my_data="前台变量";
my_data=escape(my_data)+"";//编码,防止汉字乱码
$.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){//如果调用php成功
alert(unescape(data));//解码,显示汉字
}
});
});
$backValue=$_POST['trans_data']; echo $backValue."+后台返回";

3.php Conversion processing

##json_decode ( string $json [, bool $assoc ] ) ; //Accept a

string in JSON format and convert it into a PHP variablejson_decode($data,true);

json is a string in json string format to be decoded. assoc When this parameter is TRUE, an array is returned instead of an object.

json_encode ( mixed $value [, int $options = 0 ] ) Returns the JSON form of value

json_encode($a)

I believe you have mastered the method after reading the case in this article , for more exciting content, please pay attention to other related articles on the php Chinese website!

Recommended reading:

ajax implementation to verify user name and password in the database

ajax file upload + processing browser compatibility

The above is the detailed content of Discussion and research on Ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn