Home > Web Front-end > JS Tutorial > Discuss some minor issues in Ajax

Discuss some minor issues in Ajax

亚连
Release: 2018-05-24 10:19:57
Original
1456 people have browsed it

We often encounter various problems during the use of ajax. Today I will share with you some small problems in ajax, which are more practical. Friends who are interested can refer to it

1.ajax cross-domain transfer value is required. The type of return is jsonp

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

dataType

Type: String

Expected server The data type returned. 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. The data returned by the server will then be parsed according to the

value and then 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 automatically cached. 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 get 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));//解码,显示汉字
}
});
});
Copy after login

php code

$backValue=$_POST['trans_data']; echo $backValue."+后台返回";
Copy after login

3.php json data conversion processing

json_decode ( string $json [, bool $assoc ] ); //Accept a JSON format character string and convert it into a PHP variable
json_decode($data,true);

json The json string format string 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)

The above is what I compiled for everyone. I hope that in the future It will be helpful to everyone.

Related articles:

Ajax cache problem under IE8/IE9

The problem that IE8 cannot refresh every time when using ajax access

Ajax caching problems and solutions under IE8

The above is the detailed content of Discuss some minor issues in Ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template