I'm trying to post data via ajax, this is my info:
我正在尝试通过ajax发布数据,这是我的信息:
var jsondata =
{"address" : [
{ "id": addid, "streetaddress": streetaddress, "city": city, "state": state, "zipcode": zipcode, "latitude": latitude},
]
};
var jsontosend = JSON.stringify(jsondata, null, 2);
ajax function:
ajax功能:
$.ajax({
type: "POST",
url: "process.php",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
data: jsontosend,
success: function(msg){
alert(msg);
}
});
return false;
alert('Data sent');
}
}
on the php end, when i print_r($_POST) it just says
在php结束时,当我print_r($ _ POST)它只是说
array(0) {
}
I'm alerting (jsontosend) and its showing me everything perfectly, as well as in firebug using post mothod, its showing all the params sent in a perfect clean manner.
我正在警告(jsontosend)并且它完美地展示了我的一切,以及使用post mothod的firebug,它以完美清洁的方式显示所有的params。
The only way it passes any data is if I use GET method.
它传递任何数据的唯一方法是使用GET方法。
Any advice is greatly appreciated!
任何意见是极大的赞赏!
EDIT: adding POST data from firebug. this is whats being alerted from the alert function:
编辑:从firebug添加POST数据。这是从警报功能提醒的:
{"address":[{"id":1473294,"streetaddress":"3784 Howard Ave","city":"Washington DC","state":"DC","zipcode":20895,"latitude":39.027820587}]}
this is what firebug is showing as whats being passed when using POST method:
这是firebug在使用POST方法时传递的内容:
myData=%7B%0A++++%22address%22%3A+%5B%0A++++++++%7B%0A++++++++++++%22id%22%3A+76076%2C%0A++++++++++++%22streetaddress%22%3A+%223784+Howard+Ave%22%2C%0A++++++++++++%22city%22%3A+%22Washington+DC%22%2C%0A++++++++++++%22state%22%3A+%22DC%22%2C%0A++++++++++++%22zipcode%22%3A+20895%2C%0A++++++++++++%22latitude%22%3A+39.027820587%0A++++++++%7D%0A++++%5D%0A%7D
and this is the response for var_dump of $_POST:
这是$ _POST的var_dump的响应:
array(0) {
}
}
this is a var_dump of $_POST['myData']
这是$ _POST ['myData']的var_dump
NULL
7 个解决方案
#1
8
I'm skeptical of the way you're using the contentType property. Try taking out contentType. The default content type is application/x-www-form-urlencoded (http://api.jquery.com/jQuery.ajax/).
我对你使用contentType属性的方式持怀疑态度。尝试取出contentType。默认内容类型是application / x-www-form-urlencoded(http://api.jquery.com/jQuery.ajax/)。
Also, use something like {mydata: jsontosend} for your data property.
另外,为您的数据属性使用{mydata:jsontosend}之类的东西。
$.ajax({
type: "POST",
url: "process.php",
//contentType: "application/json; charset=utf-8",
dataType: "JSON",
data: {mydata: jsontosend},
success: function(msg){
alert(msg);
}
});
#2
2
Use
使用
data:{myData: jsontosend}
数据:{myData:jsontosend}
It should send myData as a parameter in your request.
它应该将myData作为参数发送到您的请求中。
#3
2
PHP doesn't understand application/json requests (by default). See this question: How to post JSON to PHP with curl
PHP无法理解application / json请求(默认情况下)。看到这个问题:如何使用curl将JSON发布到PHP
#4
2
I found that the comment provided by dkulkarni was the best solution here. It is necessary to read directly from the input stream to get POST data of any complexity. Changing the value of $.ajax({contentType: ''})
did not work for me..
我发现dkulkarni提供的评论是最好的解决方案。有必要直接从输入流中读取以获得任何复杂性的POST数据。更改$ .ajax({contentType:''})的值对我来说不起作用..
In order to troubleshoot this problem I had set up a controller method to echo back the JSON I was posting to my server. Changing the ContentType header made no difference. But after reading dkulkarni's comment I changed my server code from this:
为了解决这个问题我设置了一个控制器方法来回显我发布到我的服务器的JSON。更改ContentType标头没有任何区别。但在阅读dkulkarni的评论后,我改变了我的服务器代码:
return $_POST;
to this:
对此:
$data = file_get_contents('php://input');
return json_decode($data);
It worked perfectly.
它工作得很好。
#5
1
As dkulkarni commented in his post (Jan 24 at 7:53), the only way to retrieve JSON sent with Content-Type application/json seems to be to retrieve it directly from the input stream.
正如dkulkarni在他的帖子中评论的那样(1月24日7:53),检索使用Content-Type application / json发送的JSON的唯一方法似乎是直接从输入流中检索它。
I made a lot of tests with different approaches, but always when I set Content-Type to JSON, the $_POST in PHP is empty.
我用不同的方法做了很多测试,但是当我将Content-Type设置为JSON时,PHP中的$ _POST是空的。
My problem is I need to set Content-Type to JSON, because the server (where I don't have access) expects it, otherwise it returns unsupported datatype. I suppose they are retrieving directly from input stream.
我的问题是我需要将Content-Type设置为JSON,因为服务器(我没有访问权限)需要它,否则返回不支持的数据类型。我想他们是直接从输入流中检索的。
Why I have to reproduce that in own PHP file? Because I need to make a proxy for testing issues, but thats not the point of this thread.
为什么我必须在自己的PHP文件中重现它?因为我需要为测试问题制作一个代理,但那不是这个线程的重点。
#6
0
Try removing ' dataType: "JSON" ' from the ajax request and check.
尝试从ajax请求中删除'dataType:“JSON”'并检查。
#7
0
Taking contentType out is not a good idea. You DO want to send this data in JSON format. Key is to do what has been mentioned above, instead of trying to access the data on the receiving end (the target php file) with $data= $_POST it is critical to use $data = file_get_contents('php://input');
将contentType取出并不是一个好主意。您想要以JSON格式发送此数据。关键是要做上面提到的,而不是尝试使用$ data = $ _POST访问接收端(目标php文件)上的数据,使用$ data = file_get_contents('php:// input')至关重要);
Here is an example of the entire round trip for a scenario with php and knockout.js:
以下是使用php和knockout.js的场景的整个往返示例:
JS (on requesting page):
JS(在请求页面上):
function() {
var payload = ko.toJSON({ YOUR KNOCKOUT OBJECT });
jQuery.ajax({
url: 'target.php',
type: "POST",
data: payload,
datatype: "json",
processData: false,
contentType: "application/json; charset=utf-8",
success: function (result) {
alert(result);
}
});
};
And then in target.php (the page receiving the request):
然后在target.php(接收请求的页面)中:
$data = file_get_contents('php://input');
$payload_jsonDecoded = json_decode($data
#1
8
I'm skeptical of the way you're using the contentType property. Try taking out contentType. The default content type is application/x-www-form-urlencoded (http://api.jquery.com/jQuery.ajax/).
我对你使用contentType属性的方式持怀疑态度。尝试取出contentType。默认内容类型是application / x-www-form-urlencoded(http://api.jquery.com/jQuery.ajax/)。
Also, use something like {mydata: jsontosend} for your data property.
另外,为您的数据属性使用{mydata:jsontosend}之类的东西。
$.ajax({
type: "POST",
url: "process.php",
//contentType: "application/json; charset=utf-8",
dataType: "JSON",
data: {mydata: jsontosend},
success: function(msg){
alert(msg);
}
});
#2
2
Use
使用
data:{myData: jsontosend}
数据:{myData:jsontosend}
It should send myData as a parameter in your request.
它应该将myData作为参数发送到您的请求中。
#3
2
PHP doesn't understand application/json requests (by default). See this question: How to post JSON to PHP with curl
PHP无法理解application / json请求(默认情况下)。看到这个问题:如何使用curl将JSON发布到PHP
#4
2
I found that the comment provided by dkulkarni was the best solution here. It is necessary to read directly from the input stream to get POST data of any complexity. Changing the value of $.ajax({contentType: ''})
did not work for me..
我发现dkulkarni提供的评论是最好的解决方案。有必要直接从输入流中读取以获得任何复杂性的POST数据。更改$ .ajax({contentType:''})的值对我来说不起作用..
In order to troubleshoot this problem I had set up a controller method to echo back the JSON I was posting to my server. Changing the ContentType header made no difference. But after reading dkulkarni's comment I changed my server code from this:
为了解决这个问题我设置了一个控制器方法来回显我发布到我的服务器的JSON。更改ContentType标头没有任何区别。但在阅读dkulkarni的评论后,我改变了我的服务器代码:
return $_POST;
to this:
对此:
$data = file_get_contents('php://input');
return json_decode($data);
It worked perfectly.
它工作得很好。
#5
1
As dkulkarni commented in his post (Jan 24 at 7:53), the only way to retrieve JSON sent with Content-Type application/json seems to be to retrieve it directly from the input stream.
正如dkulkarni在他的帖子中评论的那样(1月24日7:53),检索使用Content-Type application / json发送的JSON的唯一方法似乎是直接从输入流中检索它。
I made a lot of tests with different approaches, but always when I set Content-Type to JSON, the $_POST in PHP is empty.
我用不同的方法做了很多测试,但是当我将Content-Type设置为JSON时,PHP中的$ _POST是空的。
My problem is I need to set Content-Type to JSON, because the server (where I don't have access) expects it, otherwise it returns unsupported datatype. I suppose they are retrieving directly from input stream.
我的问题是我需要将Content-Type设置为JSON,因为服务器(我没有访问权限)需要它,否则返回不支持的数据类型。我想他们是直接从输入流中检索的。
Why I have to reproduce that in own PHP file? Because I need to make a proxy for testing issues, but thats not the point of this thread.
为什么我必须在自己的PHP文件中重现它?因为我需要为测试问题制作一个代理,但那不是这个线程的重点。
#6
0
Try removing ' dataType: "JSON" ' from the ajax request and check.
尝试从ajax请求中删除'dataType:“JSON”'并检查。
#7
0
Taking contentType out is not a good idea. You DO want to send this data in JSON format. Key is to do what has been mentioned above, instead of trying to access the data on the receiving end (the target php file) with $data= $_POST it is critical to use $data = file_get_contents('php://input');
将contentType取出并不是一个好主意。您想要以JSON格式发送此数据。关键是要做上面提到的,而不是尝试使用$ data = $ _POST访问接收端(目标php文件)上的数据,使用$ data = file_get_contents('php:// input')至关重要);
Here is an example of the entire round trip for a scenario with php and knockout.js:
以下是使用php和knockout.js的场景的整个往返示例:
JS (on requesting page):
JS(在请求页面上):
function() {
var payload = ko.toJSON({ YOUR KNOCKOUT OBJECT });
jQuery.ajax({
url: 'target.php',
type: "POST",
data: payload,
datatype: "json",
processData: false,
contentType: "application/json; charset=utf-8",
success: function (result) {
alert(result);
}
});
};
And then in target.php (the page receiving the request):
然后在target.php(接收请求的页面)中:
$data = file_get_contents('php://input');
$payload_jsonDecoded = json_decode($data