christinezeller

POST using dojo

Discussion created by christinezeller on Oct 7, 2013
I'm trying to work through the dojo.xhrPost call to send some JSON data Example - http://livedocs.dojotoolkit.org/dojo/xhrPost

I'm trying to post / send data to ESRI rest services/server.  I want to get this example working so I can move on and send things without using the editing tools.

I'm able to use Google Postman and get my post working properly.

Now I�??m trying to construct some code to handle this. 

I took the example and got it running as is.  Now I�??m trying to place my URL in the example and my content (post Data) in the example and get it running.

When I attempt to do this I get confused on several points and I was hoping someone could explain these thing to me.

The first one is in the header there is a script src='/_static/dojo/dojo.js' 
I had to change the script to use the full path name because it is not on my server.  So this made the example work. If I eliminate this script all together nothing happens so I'm wondering do I need this script, if so should I write my own or use the one in the example.  Not sure what  this is exactly.

Next question is about the same thing but in the function.  In the example in the url it was referencing the above script.  I changed this to my rest end point but not sure if I also need to reference the script.

Would someone be kind enough to explain to me what is going on and how I should more forward.  I'm green and have been stuck for a week or two now.  I'm wondering if anyone else has done anything similar.


<!DOCTYPE html>
<html >
<head>

 <link rel="stylesheet" href="/_static/dojo/../dijit/themes/claro/claro.css">
 
 <script>dojoConfig = {parseOnLoad: true}</script>
 <script src='http://livedocs.dojotoolkit.org/_static/dojo/dojo.js'></script>  <!-- NOT SURE IF I SHOULD HAVE CHANGED THIS THE EXAMPLE IS src='/_static/dojo/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 it to
    // Also, how to handle the return and callbacks.
    var xhrArgs = {
      url: "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/FeatureServer/0/applyEdits",   // The example had the url as url: "/_static/dojo/../dojo/dojo.js",  NOT sure if I need /dojo/dojo.js
      postData: dojo.toJson([{"geometry":{"x":-13085962.2699721,"y":4061306.82523268,"spatialReference":{"wkid":102100}},"attributes":{"description":"Test what","type":5}},{"geometry":{"x":-13072136.6421672,"y":4073467.58686212,"spatialReference":{"wkid":102100}},"attributes":{"description":"christineTeST","type":0}}]),
      handleAs: "text",
      load: function(data){
        dojo.byId("response2").innerHTML = "Message posted.";
      },
      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>
</head>
<body class="claro">
    <b>Push the button to POST some JSON.</b>
<br>
<br>
<button data-dojo-type="dijit/form/Button" id="submitButton2">Send it!</button>
<br>
<br>
<b>Result</b>
<div id="response2"></div>
</body>
</html>



Thanks
Christine

Outcomes