Solution to the problem that jquery cannot receive data: 1. Set "contentType:"application/x-www-form-urlencoded""; 2. Just check the post and get statements.
The operating environment of this article: Windows7 system, jquery3.2.1, Dell G3 computer.
What should I do if jquery cannot receive data? jQuery ajax cannot get data from the background?
ajax post data cannot obtain data, pay attention to the setting of content-type and post/get
About jQuery data passing data. There are various data that cannot be obtained online, garbled characters, etc.
Okay, I also encountered it today. I searched all kinds of tangles online. No matter if you look at the garbled code first, you cannot get the data.
Because I have always used jQuery ajax get to pass parameters, the value of contentType has not been set by default.
1: var Skip = 49; //Number of skipped row 2: var Take = 14; // 3: function Load(Skip, Take) { 4: $('#pPostsLoader').html('<img src="ProgressBar/ajax-loader.gif">'); 5: //send a query to server side to present new content 6: $.ajax({ 7: type: "get", 8: url: "AjaxImage.ashx", 9: data: { Skip: Skip, Take: Take }, 10: //contentType: "application/json; charset=utf-8",//(可以) 11: //contentType: "text/xml",//(可以) 12: //contentType:"application/x-www-form-urlencoded",//(可以) 13: //dataType: "string", 14: success: function (data) { 15: if (data != "") { 16: $('.thumb').append(data); 17: } 18: $('#pPostsLoader').empty(); 19: } 20: }) 21: };
Under chrome, the value of contentType is not set. OK, let’s look at the default situation in jquery:
The default parameters are passed through the url parameter, request Content type: application/x-www-form-urlencoded
General processing file acquisition parameter content:
int Skip = Convert.ToInt32(context.Request["Skip"]); 2: int Take = Convert.ToInt32(context.Request["Take"]);
No pressure, because I have always done this without any problems. Okay, let’s change the content type of the request:
1: //contentType: "application/json; charset=utf-8",//(可以) 2: //contentType: "text/xml",//(可以)
<p>也都可以,参数获取正常。<br/></p>
This is what we call the get method. The parameters follow the url and have nothing to do with Content-Type.
可是今天要用post方式了有木有。
1: $.ajax({ 2: type: "post",
<p>chrome下,没有设置contentType的值,来看默认情况:<br/></p>
data data is submitted from the form, the requested content type: application/x-www-form-urlencoded,
Okay, by default, it is also possible to obtain parameters by generally processing files.
However, what I initially set was contentType: "application/ json; charset=utf-8", look at the picture:
What is Request Paload???
Debug it, look at our from, there is no Content:
Tested:
1: //contentType: "application/json; charset=utf-8",//(不可以) 2: //contentType: "text/xml",//(不可以) 3: contentType:"application/x-www-form-urlencoded",//(可以)
<p>总结一下吧:本来get/post方式都是知道的,但注意,contentType与传递数据匹配(本文data)。<br/></p>
<p> 做过模拟登录、模拟提交数据的同学肯定都很清楚了。<br/></p>
Recommended learning: "jquery video tutorial"
The above is the detailed content of What should I do if jquery cannot receive data?. For more information, please follow other related articles on the PHP Chinese website!