how to get values returned by esriRequest request Succeeded

956
3
Jump to solution
12-05-2016 10:44 AM
JoseSanchez
Occasional Contributor III

Hello everyone,

 I am testing a rest web service created in ASP.Net.

The rest service http://localhost:61004/api/value   returns one value:

  "value1"

I am testing also how to call this Restful service from esri javascript 1.38. This is the sample called "requests JSON using esriRequest".

https://developers.arcgis.com/javascript/3/jssamples/data_requestJson.html

I just copied it and set up the proxy and changed the default url

esriConfig.defaults.io.proxyUrl = "/proxy/";

dom.byId("url").value = "http://localhost:61004/api/aasis";

and it works!!!

My question now, is how do I get the content of "response" in a local variable to process it. My goal is to save this value in a field, but I do not know how to retrieve it.

     function requestSucceeded(response, io) {

               dom.byId("status").innerHTML = "";

               dojoJson.toJsonIndentStr = " ";

               dom.byId("content").value = dojoJson.toJson(response, true);   <<===============

           }

      

 Thanks

0 Kudos
1 Solution

Accepted Solutions
RichardAbram
New Contributor III

You would need to set that value in the requestSucceeded function. 

function requestSucceeded(response, io) {

dom.byId("status").innerHTML = "";

dojoJson.toJsonIndentStr = " ";

dom.byId("content").value = dojoJson.toJson(response, true);

if(evt.adds[0].attributes.hasOwnProperty('COMMENTS')){
                  evt.adds[0].attributes.COMMENTS =  response;

   }

}

View solution in original post

0 Kudos
3 Replies
RichardAbram
New Contributor III

the variable "response" is the value if the return is just text.  Do you send back a json string or just a value?  So is the web service returning text "value1" or something like this json string {'value':'value1'}?  If it is a json string then you need to parse it and reference the name otherwise use the value of response.

If you run this in IE or Chrome and put a break point in the requestSucceeded function you can see the format of the value that was returned

0 Kudos
JoseSanchez
Occasional Contributor III

Hi,

What is the best way to save in the field "evt.adds[0].attributes.COMMENTS"  the value "response" returned?

if(evt.adds[0].attributes.hasOwnProperty('COMMENTS')){
                  evt.adds[0].attributes.COMMENTS =   ????? ;  <<== value "response" from  function requestSucceeded

                }

=======================================================================

function getContent() {

var result = "";

var contentDiv = dom.byId("content");

contentDiv.value = "";

domClass.remove(contentDiv, "failure");

dom.byId("status").innerHTML = "Downloading...";

var requestHandle = esriRequest({

"url": dom.byId("url").value,

"handleAs": "json",

"callbackParamName": "jsoncallback"

});

requestHandle.then(requestSucceeded, requestFailed);

}

function requestSucceeded(response, io) {

dom.byId("status").innerHTML = "";

dojoJson.toJsonIndentStr = " ";

dom.byId("content").value = dojoJson.toJson(response, true);

}

function requestFailed(error, io) {

domClass.add(dom.byId("content"), "failure");

dom.byId("status").innerHTML = "";

dojoJson.toJsonIndentStr = " ";

dom.byId("content").value = dojoJson.toJson(error, true);

}

0 Kudos
RichardAbram
New Contributor III

You would need to set that value in the requestSucceeded function. 

function requestSucceeded(response, io) {

dom.byId("status").innerHTML = "";

dojoJson.toJsonIndentStr = " ";

dom.byId("content").value = dojoJson.toJson(response, true);

if(evt.adds[0].attributes.hasOwnProperty('COMMENTS')){
                  evt.adds[0].attributes.COMMENTS =  response;

   }

}

0 Kudos