Note that there are some explanatory texts on larger screens.

plurals
  1. PODojo dojo.rawXhrPost and dojo.xhrPost
    text
    copied!<p>My question is: can we use dojo.xhrPost to post some Json data? More detail:</p> <p>I have been experimenting with Dojo code to POST JSON data to a RESTful service. It seems to be that the behaviours of <strong>dojo.xhrPost</strong> and <strong>dojo.rawXhrPost</strong> are different, or to be more accurate rawXhrPost() works and xhrPost() does not. This is not consistent with my reading of the <a href="http://docs.dojocampus.org/dojo/rawXhrPost" rel="noreferrer">docs</a></p> <blockquote> <p>The original purpose of dojo.rawXhrPost was a method that could be used to send a raw post body to the server. As of 1.3, this function is common with dojo.xhrPost(). So, for usage of dojo.rawXhrPost(), see dojo.xhrPost()</p> </blockquote> <p>Which implies that xhrPost() is enough. My code looks like this - I've got a "toy" library service that manages Editions of Books. The code wants to POST a new entry,</p> <pre><code> var myEdition = {"Edition":{"isbn":"44"}}; var xhrArgs = { url: "http://localhost:8081/LibraryWink/library/editions", postData: dojo.toJson(myEdition), handleAs: "json", headers: { "Content-Type": "application/json"}, load: function(data) { dojo.byId("mainMessageText").innerHTML = "Message posted."; }, error: function(error) { dojo.byId("mainMessageText").innerHTML = "Error :" + error; } }; var deferred = dojo.rawXhrPost(xhrArgs); </code></pre> <p>The <em>headers: { "Content-Type": "application/json"}</em> part in necessary so that my JAX-RC service understands that the content is JSON.</p> <p>What I find is that the code above works perfectly. However if instead I say:</p> <pre><code>var deferred = dojo.xhrPost(xhrArgs); </code></pre> <p>No data is transmitted in the POST. I have a TCP/IP monitor in place and can see that there is nothing transmitted.</p> <p>So, is this a bug, or am I driving xhrPost() incorrectly? Or should I use rawXhrPost()? If the latter, under what circumstances do we use the two flavours of XhrPost?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload