As the title suggests, I am currently implementing the new syntax for dojo/request/xhr. However, I have encountered an issue where the data does not load properly and throws an error. Surprisingly, using the old syntax with the same URL yields the desired result.
Here is the code snippet with the current syntax that is causing the problem:
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);
});
};
Although the data is correctly logged in the console, the xhr request produces the following 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)"
In contrast, the following old syntax works flawlessly:
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 the JSON data being used:
{ "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" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddb9ba9daeb2b0b8b9b2b0bcb4b3f3beb2b0">[email protected]</a>",
"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" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3756535a5e597744585a5253585a565e591954585a">[email protected]</a>",
"members" : [ "M 11",
"M 12"
],
"name" : "Dep3",
"parent" : "Dep1"
},
{ "chief" : "M 11",
"email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ceada1a3a3abbcada7afa28ebda1a3abaaa1a3afa7a0e0ada1a3">[email protected]</a>",
"members" : [ "M 21",
"M 22"
],
"name" : "Dep4",
"parent" : "Dep1"
}
],
"orgaTestModel" : { "corporation" : "Corporation Sample",
"name" : "Orga Sample 7855"
},
"root" : "Dep1"
}
Note:
I am utilizing version dojo 1.8.1
, but I attempted to run it with dojo 1.9.2
as well without success. If anyone can identify the issue in my code or suggest a solution, I would greatly appreciate it.
Your assistance is highly valued.