admin管理员组文章数量:1279021
I am trying to send a post param. to request.php
but it returns that the post param. are empty.
<script src=".1.0/jquery.min.js"></script>
$.ajax({
url: "request.php",
type: "POST",
data: "{key:'123', action:'getorders'}",
contentType: "multipart/form-data",
plete: alert("plete"),
success: function(data) {
alert(data);
},
error: alert("error")
});
I am trying to send a post param. to request.php
but it returns that the post param. are empty.
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js"></script>
$.ajax({
url: "request.php",
type: "POST",
data: "{key:'123', action:'getorders'}",
contentType: "multipart/form-data",
plete: alert("plete"),
success: function(data) {
alert(data);
},
error: alert("error")
});
Share
Improve this question
edited Oct 28, 2017 at 17:36
PaulE
asked Aug 11, 2016 at 4:08
PaulEPaulE
832 silver badges9 bronze badges
4
- try data : {'key':'123', 'action':'getorders'} – Vishnu Commented Aug 11, 2016 at 4:13
-
remove
" "
from this data formatdata:"{key:'123', action:'getorders'}"
– Jack jdeoel Commented Aug 11, 2016 at 4:13 - didn't work @DavidJawphan – PaulE Commented Aug 11, 2016 at 17:10
- @Vishnu same prob.. key is empty – PaulE Commented Aug 11, 2016 at 17:11
5 Answers
Reset to default 5remove " " from this data as data:{key:'123', action:'getorders'}
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$.ajax({
url:"request.php",
type:"POST",
data:{key:'123', action:'getorders'},
contentType:"multipart/form-data",
plete:alert("plete"),
success:function(data) {
alert(data);
},
error:alert("error")
});
</script>
You must use FormData
for multipart/form-data
,and also need additional option in ajax ..
var request = new FormData();
request.append('key',123);
request.append('action','getorders');
$.ajax({
url: "request.php",
type: "POST",
data: request,
processData : false,
contentType: false,
success: function(data) {
alert(data);
}
});
This will help you. You don't want a string, you really want a JS map of key value pairs.
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$.ajax({
url:"request.php",
type:"POST",
data:{key:'123', action:'getorders'},
contentType:"multipart/form-data",
plete:alert("plete"),
success:function(data) {
alert(data);
},
error:function(){
alert("error");
});
</script>
This should work like a champ ,
construct object as below and stringify
it as JSON.stringify(newObject)
then there will be no chance of error
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
var newObject= new Object();
newObject.key= '123';
newObject.action='getorders'
$.ajax({
url:"request.php",
type:"POST",
data:JSON.stringify(newObject),
contentType:"multipart/form-data",
plete:alert("plete"),
success:function(data) {
alert(data);
},
error:function(){
alert("error");
});
</script>
Try this:
data: JSON.stringify({key: '123', action: 'getorders'}),
contentType: "application/json"
本文标签: JavaScript jQuery AJAX POST data errorStack Overflow
版权声明:本文标题:JavaScript jQuery AJAX POST data error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741254119a2366341.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论