Select to view content in your preferred language

How do I assign the esriRequest response to a variable

1560
4
06-10-2014 09:00 PM
JinnanZhang
Emerging Contributor
Hi guys,

I have the code below, it's pretty straightforward and what it dose is making an esriRequest and then assign the serviceDescription of a Feature Layer from the response to a variable.

function getFeatureServerDescription(url){

    var serviceDescriptionStr;

    require(["esri/request"], function(esriRequest) {
          var layersRequest = esriRequest({
            url: url,
            content: { f: "json" },
            handleAs: "json",
            sync: true,
            callbackParamName: "callback"
          });
          
          layersRequest.then(requestSucceeded, requestFailed);
    });

    function requestSucceeded(response){

      serviceDescriptionStr = response.serviceDescription;
      console.log('Value from the original response: ' + serviceDescriptionStr);
    }

    function requestFailed(error){

      console.log("Error: ", error.message);
      serviceDescription = "";

    }   

    console.log('Value that will be returned to browser: ' + serviceDescriptionStr);
    return serviceDescriptionStr;
}


It works fine in Chrome, Firefox and IE 10, the service description is returned and I can see two messages from the console:

>Value from the original response: some value

>Value that will be returned to browser: some value

But issue comes when I run it in IE 9 or 8, it won't assign the response two the variable outside of the esri request object, I see two messages from the console like:

>Value that will be returned to browser: undefined

>Value from the original response: some value

I know it's caused by the non-blocking nature of JavaScript so the code keeps moving along even the response has not come to the callback yet, which made myVariable at the end of the code as undefined.

But what I can't understand is why it's working fine in Chrome and Firefox, why would Chrome wait till the response is received and then move along to execute rest of code.

So my question is how can I make it work in IE 9?

Thanks a lot!
0 Kudos
4 Replies
MuditAgarwal
Occasional Contributor
Please try below code. Same is tested in IE 8.

var serviceDescriptionStr;
        var layersRequest = esri.request({
            url: "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapSer...",
            content: { f: "json" },
            handleAs: "json",
            sync: true,
            callbackParamName: "callback",
            load: function (result) {
                serviceDescriptionStr = result.serviceDescription;
            },
            error: function (error) {
                alert(error);
            }
0 Kudos
JinnanZhang
Emerging Contributor
Thanks a lot for your reply, I tested the code by using the way you suggested in both IE 8 and IE 9 and I am still getting the same issue that it won't assign the response into the variable serviceDescriptionStr, which means I still see the undefined variable at the end of the code in the console.

Any thought?

Thanks.

Please try below code. Same is tested in IE 8.

var serviceDescriptionStr;
        var layersRequest = esri.request({
            url: "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapServer/0",
            content: { f: "json" },
            handleAs: "json",
            sync: true,
            callbackParamName: "callback",
            load: function (result) {
                serviceDescriptionStr = response.serviceDescription;
            },
            error: function (error) {
                alert(error);
            }
0 Kudos
MuditAgarwal
Occasional Contributor
Thanks a lot for your reply, I tested the code by using the way you suggested in both IE 8 and IE 9 and I am still getting the same issue that it won't assign the response into the variable serviceDescriptionStr, which means I still see the undefined variable at the end of the code in the console.

Any thought?

Thanks.


Hi,
I did some changes and configured the code on JS FIDDLE. Here is the Link and same is tested in IE 8 and chrome.


Please try again.

Thanks
Mudit
0 Kudos
JinnanZhang
Emerging Contributor
Mudit,

Thanks a lot for helping on this, really appreciate.

However, I made a tiny change to your code that I put another alert at the end of the code.

If you run it, you will see the alert with an undefined value first then the alert from the request. I know it's silly but what if I have to pass the value to the alert at the end? How can I do it?

Here is my updated code, thanks:

dojo.require("esri.map");

function init() {
   var serviceDescriptionStr;
        var layersRequest = esri.request({
            url: "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapServer",
            content: { f: "json" },
            handleAs: "json",
            sync: true,
            callbackParamName: "callback",
            load: function (result) {
                serviceDescriptionStr = result.serviceDescription;
                alert(serviceDescriptionStr);
            },
            error: function (error) {
                alert(error);
            }
        });
    alert(serviceDescriptionStr); //added by jinnan
}
dojo.ready(init);


Hi,
I did some changes and configured the code on JS FIDDLE. Here is the Link and same is tested in IE 8 and chrome.


Please try again.

Thanks
Mudit
0 Kudos