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 astring in JSON format and convert it into a PHP variablejson_decode($data,true);
json_encode($a)
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!