Php 如何正确使用dojo.xhrPost?
这是我的JS:Php 如何正确使用dojo.xhrPost?,php,javascript,ajax,dojo,Php,Javascript,Ajax,Dojo,这是我的JS: <script> dojo.require("dijit.form.Button"); function sendText(){ var button = dijit.byId("submitButton2"); dojo.connect(button, "onClick", function(event){ // The parameters to pass to xhrPost, the message, and the url to send
<script>
dojo.require("dijit.form.Button");
function sendText(){
var button = dijit.byId("submitButton2");
dojo.connect(button, "onClick", function(event){
// The parameters to pass to xhrPost, the message, and the url to send it to
// Also, how to handle the return and callbacks.
var xhrArgs = {
//type: "POST",
url: "http://testjson.php",
content: dojo.toJson({key1:"value1",key2:"value2"},true),
handleAs: "text",
load: function(newContent){
dojo.byId("response2").innerHTML = newContent;
},
error: function(error){
// We'll 404 in the demo, but that's okay. We don't have a 'postIt' service on the
// docs server.
dojo.byId("response2").innerHTML = "Message posted.";
}
}
dojo.byId("response2").innerHTML = "Message being sent..."
// Call the asynchronous xhrPost
var deferred = dojo.xhrPost(xhrArgs);
});
}
dojo.ready(sendText);
</script>
require(“dijit.form.Button”);
函数sendText(){
var按钮=dijit.byId(“submitButton2”);
connect(按钮,“onClick”,函数(事件){
//要传递给xhrPost的参数、消息以及要将其发送到的url
//还有,如何处理返回和回调。
变量xhrags={
//类型:“POST”,
url:“http://testjson.php",
内容:dojo.toJson({key1:value1',key2:value2},true),
handleAs:“文本”,
加载:函数(newContent){
dojo.byId(“response2”).innerHTML=newContent;
},
错误:函数(错误){
//我们将在演示中使用404,但没关系。我们在屏幕上没有“POSIT”服务
//文档服务器。
dojo.byId(“response2”).innerHTML=“消息已发布。”;
}
}
dojo.byId(“response2”).innerHTML=“正在发送的消息…”
//调用异步xhrPost
var deferred=dojo.xhrPost(xhrags);
});
}
准备就绪(sendText);
以下是我的PHP:
<?php
foreach($_POST as $key => $val) echo '$_POST["'.$key.'"]='.$val.'<br />';
?>
问题是什么也没有归还。
如果我把content
而不是postData
放进去,我会把$\u POST[0]='{',$\u POST[1]='k'等字符逐个放进去,限制在1000个字符以内。这是个大问题
有人能告诉我我做错了什么吗?我从dojo网站上得到了正确的代码,所以应该没问题。我相信您的
内容
正在逐字符发送,因为您正在将内容对象转换为JSON。根据,内容
属性应该是一个JavaScript对象希望这有助于解决您的问题
应该注意的是,这个模块不赞成使用,所以最好使用它,除非您有较低的版本要求。php
$\u POST
数组只显示表单编码的数据。在您的示例中,您发布的是json,因此它不会直接显示在$\u POST
中
这里有几个选项。您可以继续将数据作为json发布,并直接从php输入流读取发布的json:$data=json\u decode(file\u get\u contents('php://input“));
。这可能是最简单的,它取代了访问数据的$\u POST
数组
其他选项包括不发布json(只发送表单编码数据)和将json作为表单编码数据发布:
在这种情况下,您的内容将变成
content:'my_post_data='+dojo.toJson({key1:value1',key2:value2},true),
(您可能需要更改handleAs
fyi)
然后在服务器端,您可能会看到如下内容
$\u POST['my\u POST\u data']='{“key1”:“value1”,“key2”:“value2”}'
可以通过json\u decode()
thx进行处理。为了避免大小问题,我以某种方式成功地逐项传输数据。