dojo JsonRest call not working

101 views Asked by At

I'm trying to call my RESTful service from dojo. All I can see from debugger is, it tries to call the service but it doen't reach there. There are no errors. I can see the 'hello' alert.

    define(["dojo/store/JsonRest","dojo/domReady!"],
       function(JsonRest){
             alert("Hello");
             var rest = new JsonRest({
                target: "/my/rest/call"
              });
       }
);

I's following this page from dojotoolkit.

But if i call using a declare then it works.

define(["dojo/store/JsonRest","dojo/_base/declare","dojo/domReady!"],
       function(JsonRest, declare){
            var rest = declare(JsonRest);
            var restResult = new rest({
                target: "/my/rest/call"
            }); 
       }
);

What am I doing wrong here?

error messages in console:

enter image description here

1

There are 1 answers

3
iH8 On

You're not following that tutorial to the letter. The difference is that you're using define and not require. Dojo's define is used in combination with declare to create new Dojo classes. Dojo's require is used to load and use existing classes. The link below is a recommended read and in your case pay special attention to the 'Requiring modules' and 'Defining modules' parts:

https://dojotoolkit.org/documentation/tutorials/1.8/modules/

If you use require like in that tutorial, it works perfectly:

require([
    'dojo/store/JsonRest',
], function(
    JsonRest
) {

    new JsonRest({
        target: 'some/resource/'
    }).get(1).then(function (item) {
        alert(JSON.stringify(item));
    });

});

Here's a working example on Plunker: http://plnkr.co/edit/ZhsO67BFpWB5Txqy0Zl9?p=preview