sBoroushaki
sBoroushaki

Reputation: 89

How to parse JSON using DOJO AJAX and REST

I am trying to send a request to a REST server using DOJO AJAX, however I get a null object as result (console):

You CLICKED = click clientX=34, clientY=13 JSON loaded from server: null

Here is my code:

// JavaScript Document

// load requirements for declarative widgets in page content
require([   "dojo/parser",
        "dojo/dom",
        "dojo/domReady!",
        "dijit/form/Button"
        ]);


// Start initializing
dojo.ready(function(){

dojo.connect (
                aBut1,
                "onClick",
                function(e){

                    console.log('You CLICKED = ', e);
                    dojo.xhrGet({
                                // The URL of the request
                                url: "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/find",
                                // Handle the result as JSON data
                                handleAs: "json",
                                // content
                                content: {f:"json", searchText:"new", contains:"true", searchFields:"state_new", layers:"2", returnGeometry:"false" },
                                // The success handler
                                load: function(jsonData) {
                                    // Create a local var to append content to
                                    console.info("JSON loaded from server:  ", jsonData);

                                },
                                // The error handler
                                error: function() {
                                    console.log('JSON log Error');
                                }
                            });

                });


}); // End dojo.ready

This is the REST url I would like to use:

http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/find?searchText=new&contains=true&searchFields=state_name&sr=&layers=2&returnGeometry=false&f=json

I save the result of this URL in a json file and AJAX works reading that file and returning an object with 4 items. It is not using REST URL.

Thank you

Upvotes: 3

Views: 2922

Answers (2)

Craig Swing
Craig Swing

Reputation: 8162

I believe you are trying to use dojo.xhrGet to access a different domain, which it cannot do.

Limitations

dojo.xhrGet (and other functions in the same line: dojo.xhrPost, dojo.xhrDelete, dojo.xhrPut), are bound by the ‘same domain’ security policy of the browser. This means that they can only establish a connection back to the same server that served the HTML page. If you wish to use this API to talk to servers other than the one that originated your page, then you will have to use a proxy on your originating server and have it forward the requests. The only other solution to working around the same domain restriction is to use alternate IO methods, such as dojo.io.script.

http://dojotoolkit.org/reference-guide/1.7/dojo/xhrGet.html

Upvotes: 1

Hank Gay
Hank Gay

Reputation: 71949

That's not much to go on. What happens if you use curl to make a request to that URL?

Are you using Dojo 1.7? If so, maybe the Ajax Quickstart docs can help (I linked directly to the AMD-style, since that's probably what you should be using if you can). There are also more detailed docs on xhrGet.

Upvotes: 0

Related Questions