-1

Like the title says I am using the new dojo syntax for the dojo/request/xhr, but this doesn't seem to work and throws an error while loading data while the old syntax with the same url gives the wanted result.

This is the current syntax which doesn't load data correctly:

    this.get = function (url, loadFunc, errorFunc, handleFunc) {
        xhr( url, {
            handleAs: 'json'
        }).then(function(data){
            typeof loadFunc === 'function' ? loadFunc : function () { };
            console.log(url+ " load");
            console.log(data);
        }, function(error){
            typeof errorFunc === 'function' ? errorFunc : function () {  };
            console.log(url+" error");
            console.dir(error);
        }, function(handle){
            typeof handleFunc === 'function' ? handleFunc : function () { };
            console.log(url+" handle");
            console.log(handle);
        });
    };

As you can see I am printing the data to the console, and I am getting the right data but the xhr request throws this Error:

"SyntaxError: Unexpected token o at Object.parse (native) at l.json (http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:228:250) at m (http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:227:277) at j [as handleResponse] (http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:151:351) at XMLHttpRequest.e (http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:154:393)"

While the following old syntax works perfectly:

    this.get = function (url, loadFunc, errorFunc, handleFunc) {
      dojo.xhrGet({
            url: url,
            handleAs: 'json',
            load: typeof loadFunc === 'function' ? loadFunc : function () { },
            error: typeof errorFunc === 'function' ? errorFunc : function () { },
            handle: typeof handleFunc === 'function' ? handleFunc : function () { }
        });
    };

EDIT:

This is my JSON data:

{ "assistants" : [ { "assistants" : [ "M 11",
        "M 1"
      ],
    "name" : "M X1"
  },
  { "assistants" : [ "M 2",
        "M 2XX1",
        "M 3"
      ],
    "name" : "M 1"
  },
  { "assistants" : [ "M 2" ],
    "name" : "M 2"
  },
  { "assistants" : [  ],
    "name" : "M 3"
  }
],
"chiefs" : [ { "chief" : "M X1",
    "name" : "M 11"
  },
  { "chief" : "M 11",
    "name" : "M 1"
  },
  { "chief" : "M X1",
    "name" : "M 2"
  },
  { "chief" : "M 744X1",
    "name" : "M 3"
  }
],
"departments" : [ { "assistants" : [ "M 11",
        "M 3",
        "M 21"
      ],
    "chief" : "M X1",
    "email" : "dg@somedomain.com",
    "interim" : [ "M X542",
        "M 4"
      ],
    "members" : [ "M 2",
        "M 3",
        "M 4",
        "M 5",
        "M X24544"
      ],
    "name" : "Dep1",
    "notify" : [ "Dep2",
        "M X2",
        "M 21"
      ],
    "resp" : "M 21",
    "validators" : [ "Dep2",
        "M 2",
        "M 558"
      ]
  },
  { "chief" : "M 1",
    "email" : "admin@somedomain.com",
    "members" : [ "M 11",
        "M 12"
      ],
    "name" : "Dep3",
    "parent" : "Dep1"
  },
  { "chief" : "M 11",
    "email" : "commercial@somedomain.com",
    "members" : [ "M 21",
        "M 22"
      ],
    "name" : "Dep4",
    "parent" : "Dep1"
  }
],
 "orgaTestModel" : { "corporation" : "Corporation Sample",
  "name" : "Orga Sample 7855"
},
"root" : "Dep1"
}

Note: I am using dojo 1.8.1 version but I tested it with dojo 1.9.2 too, but it still doesn't work.

I can't figure out what's the problem. Is there something wrong with my code or it's another problem ?

Any help would be appreciated.

Vivz
  • 6,625
  • 2
  • 17
  • 33
cнŝdk
  • 31,391
  • 7
  • 56
  • 78

1 Answers1

1

You haven't provided an example of what your original JSON response looks like, but I am guessing it is not valid JSON.

dojo.xhrGet actually uses eval when handleAs is set to "json", which will by definition be more permissive than a strict JSON parser. dojo/request, on the other hand, uses JSON.parse if available, which expects JSON to be well formed (e.g. all keys are quoted strings, all strings use double quotes).

Try pasting one of your JSON responses into http://jsonlint.org/ - if it doesn't validate there, then it won't validate with dojo/request.

(The reason dojo.xhrGet uses eval, even though it's potentially unsafe, is that it was written prior to widespread JSON.parse support, and changing it would have broken backwards compatibility - in other words, even without switching APIs, developers would run into the problem you're running into right now.)

Edit: The JSON provided in the question now is valid, and works with both the old and new APIs.

Ken Franqueiro
  • 10,559
  • 2
  • 23
  • 40
  • The question is why is the old syntax working perfectly with my json data and not the new one ? I've already passed my json to http://jsonlint.org/ and http://jsonformat.com/ too, and it was not validated because it was missing the double quote wrapping inside keys, but I don't think this was the problem because even when I corrected it, it still doesn't work. – cнŝdk Apr 18 '15 at 16:13
  • Can you edit your question to include the corrected JSON (or a subset of it if it's long)? The invalid JSON likely is the problem, since as I explained, the old API was more tolerant due to how it processes the data. It's the only thing that easily comes to mind given that everything else in your scenario remains constant. – Ken Franqueiro Apr 18 '15 at 18:55
  • Thanks for your response, I included a subset of my json data as you requested. – cнŝdk Apr 20 '15 at 08:57
  • I tested with your JSON and both the old and new APIs seemed to work fine for me. https://gist.github.com/kfranqueiro/f89ffeb1f41ace33ea68 – Ken Franqueiro Apr 21 '15 at 00:03
  • Thanks for this test, it works for me too with this valid JSON but the problem is with the old JSON that works with the old syntax but not the new one. – cнŝdk Apr 22 '15 at 13:22
  • You stated before that your old JSON didn't pass validation. I explained in my answer that invalid JSON won't work with the new API. I'm not sure what's left to resolve here - either ensure your services return valid JSON (preferred), or continue using the old API. – Ken Franqueiro Apr 22 '15 at 13:35
  • Yes but I just wanted to know if there's any other solution than changing and validating the JSON data for the new API – cнŝdk Apr 23 '15 at 08:21