$ _POST中的JQuery Mobile POST数据为空 [英] JQuery Mobile POST data empty in $_POST
问题描述
我遇到了JQuery Mobile(对我来说是新的)和AJAX调用的问题.我正在使用以下代码:
I'm running into a problem with JQuery Mobile (new to me) and the AJAX-call. I'm using the following code:
$.ajax({
type: "POST",
url: "http://**correct url**/post/todoitem",
beforeSend: addHeaders,
dataType: "json",
contentType: "application/json",
data: { "todoitem":"test" }, // this is just as a test
success: function(result) {
alert("Success: " + JSON.stringify(result));
},
error: function() {
alert("Error: " + JSON.stringify(arguments));
}
});
在执行此操作时,它将调用PHP脚本,在该脚本中我需要来自todoitem的数据,因此在这种情况下,字符串"text"(最后,将发送多个变量,但是现在我只是在使用为简单起见,一个参数).
While executing this, it calls a PHP script where I need the data from the todoitem, so in this case the string "text" (in the end, multiple variables are to be send, but for now I'm just using one parameter for simplicity).
我的PHP代码如下(也仅用于测试目的):
My PHP code looks like this (also just for testing purposes):
echo json_encode($_POST));
结果是:无,为null. $ _POST似乎为空.我已经搜索并尝试了许多方法,但是大多数答案(即使在stackoverflow上也是如此)都是关于表格的,人们说我需要序列化表格的内容.但是,我根本不使用表格.
The result is: nothing, null. The $_POST seems to be empty. I've searched and tried many things, but most answers (even here on stackoverflow) are about forms and people say I need to serialize the contents of the form. However, I'm not using a form at all.
我也尝试过
data: JSON.stringify({ "todoitem" : "test" })
正如一些建议所建议的那样,但这也不起作用. 我确实知道由于我尝试过的这个小PHP黑客行为而导致数据正在传输:
as some suggested but this did not work either. I do know that the data is being transfered because of this little PHP hack I tried:
echo file_get_contents('php://input');
这恰好显示了数据:todoitem = test.那么,这一切在哪里出错呢?我已经为此工作了好几天!提前thnx
That exactly shows the data: todoitem = test. So where does this all go wrong? I'm working on this for days now! Thnx in advance
推荐答案
问题出在这部分代码上:
The problem is with this part of your code:
contentType: "application/json",`
删除该行应使发送的Content-Type
标头默认为application/x-www-form-urlencoded
,PHP会将请求解码为$_POST
.
Removing that line should make the sent Content-Type
header default to application/x-www-form-urlencoded
and PHP will decode the request into $_POST
.
这篇关于$ _POST中的JQuery Mobile POST数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!