本文介绍了Ajax POST到PHP并接收响应,jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我有一个JSON数组,我想将其发布到文件中,然后接收响应.但是它似乎不起作用,所以我希望你们能帮我一点忙.
So, I have a JSON array that I want to POST to a file and then receive a response. But somehow it doesn't seem to work, so I was hoping you guys could help me a bit out.
data = JSON.stringify({
"jsonrpc": "2.0",
"method": "login",
"id": 1,
"params": {
"params": {
"username": "1234",
"password": "4321"
}
}
});
$.ajax({
url:"functions/proxy.php",
type:"POST",
data : data,
success: function(data){/* do something*/ },
error: function(data) {/* do something*/)}
});
当我提交表单时,error function
运行,当var_dumping $_POST
时我得到此:
When I submit the form, the error function
runs, and I get this when var_dumping $_POST
:
array(0) {
}
奇怪的是,当data
看起来像这样:
Weird thing is, when data
looks like this:
data: "username=1234&password&4321"
我明白了:
Array
(
[username] => 1291
[password] => 1877
)
有人可以帮助我如何以JSON格式发送数据吗?
Could anyone help me with how I can be able to send the data in JSON format?
推荐答案
$.ajax({
url:"functions/proxy.php",
type:"POST",
data : data,
success: function(data){/* do something*/ },
error: function(data) {/* do something*/)}
});
应该是
$.ajax({
url:"functions/proxy.php",
type:"POST",
data : {mydata:data},
success: function(data){/* do something*/ },
error: function(data) {/* do something*/)}
});
尝试
print_r($_POST);
这篇关于Ajax POST到PHP并接收响应,jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!