简体   繁体   English

如何正确使用dojo.xhrPost?

[英]How to use dojo.xhrPost properly?

Here is my JS: 这是我的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 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>

Here is my PHP: 这是我的PHP:

    <?php 

foreach($_POST as $key => $val) echo '$_POST["'.$key.'"]='.$val.'<br />';

?>

The problem is that nothing is being returned. 问题是什么也没有退还。 If I put content instead of postData I have $_POST[0]='{', $_POST[1]='k' etc character by character, limited to 1000. This is a big problem. 如果我放置content而不是postData ,则postData都有$ _POST [0] ='{',$ _ postData [1] ='k'等字符,限制为1000。这是一个大问题。

Please can somebody tell me what I'm doing wrong? 请有人告诉我我做错了吗? I got this code right from the dojo website, so it should be alright. 我从dojo网站上获得了正确的代码,因此应该没问题。

I believe your content is being sent character by character because you are converting your content object into JSON. 我相信您的content正在逐字符发送,因为您正在将内容对象转换为JSON。 According to the dojo.xhrPost documentation , the content property is expected to be a JavaScript Object. 根据dojo.xhrPost文档content属性应为JavaScript对象。 I hope this helps solve your problem. 我希望这有助于解决您的问题。

It should be noted that this module is deprecated in favor of dojo/request/xhr , so it is better to use that unless you have lower version requirements. 应当注意,不推荐使用此模块,而推荐使用dojo / request / xhr ,因此最好使用它,除非您的版本要求较低。

The php $_POST array only shows form encoded data. php $_POST数组仅显示表单编码的数据。 In your example you are POSTing json, so it won't directly show up in $_POST . 在您的示例中,您正在发布json,因此它不会直接显示在$_POST

You have a couple options here. 您在这里有几个选择。 You could continue to post the data as json and read the POSTed json directly from the php input stream: $data = json_decode(file_get_contents('php://input')); 您可以继续将数据作为json发布,并直接从php输入流中读取POSTed的json: $data = json_decode(file_get_contents('php://input')); . This is probably the easiest, and it replaces accessing the $_POST array for the data. 这可能是最简单的,它代替了访问$_POST数组以获取数据。

Other options include not POSTing json (just send form encoded data) and POSTing json as form encoded data: 其他选项包括不发布json(仅发送表单编码的数据)和发布json 作为表单编码的数据:

In that case, your content would become something like 在这种情况下,您的content将变成

content: 'my_post_data='+dojo.toJson({key1:"value1",key2:"value2"}, true), (you may need to change handleAs fyi) content: 'my_post_data='+dojo.toJson({key1:"value1",key2:"value2"}, true), (您可能需要更改handleAs

Then on the server side you would likely see something like 然后在服务器端您可能会看到类似

$_POST['my_post_data']= '{"key1":"value1","key2":"value2"}' which could be processed by json_decode() $_POST['my_post_data']= '{"key1":"value1","key2":"value2"}'可以由json_decode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM