| Page 1 of 1 | [ 8 posts ] |
Leonardo Martins
|
Hi there,
I'm just beginning to learn the DataSource module and can't go any further because of this error. Does anyone know why I get the "Unexpected token :" error for the following DataSource use when I invoke the sendRequest method? Code: var datasource = new Y.DataSource.Get({ source: 'http://127.0.0.1:8000/api/v1/test/?format=json&' }); datasource.sendRequest({ request: 'q=test', callback: { success: function(e) { Y.log(e.response, 'info', 'app'); }, failure: function(e) { Y.log(e.error.message, 'error', 'app'); } } }); Am I supposed to not specify an HTTP port in the source string? I'd appreciate any help. Thanks, -- LM |
|
It looks like your JSON response may be invalid, what is the output from that url?
|
Leonardo Martins
|
Here it is as shown in my browser's debug window:
Code: {"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"actual": {"discharge": [1, 2, 3], "id": "", "inflow": [1, 2, 3], "output": [1, 2, 3], "resource_uri": "/api/v0/series/None/", "spill": [1, 2, 3]}, "day": "2012-02-03", "id": "4f2c3a811d66ac2d6d000000", "optimal": {"discharge": [1, 2, 3], "id": "", "inflow": [1, 2, 3], "output": [1, 2, 3], "resource_uri": "/api/v0/series/None/", "spill": [1, 2, 3]}, "program": {"discharge": [1, 2, 3], "id": "", "inflow": [1, 2, 3], "output": [1, 2, 3], "resource_uri": "/api/v0/series/None/", "spill": [1, 2, 3]}, "resource_uri": "/api/v0/operation/4f2c3a811d66ac2d6d000000/", "uhe": ""}]} |
|
Ok, so you're returning pure JSON. In order to do this you need to wrap that content with a function callback and use JSONP not Get.
Get just includes a script tag on the page and if that JSON content was in a script tag it would just fail, since it's JSON not JavaScript. See here: http://yuilibrary.com/yui/docs/datasource/#get |
Leonardo Martins
|
Thanks for the prompt help, Dav.
I will look into this stuff more thoroughly and post the results for other beginners who might be facing the same problem. -- LM |
Leonardo Martins
|
I was able to solve the problem by removing the "request" attribute from the sendRequest method. So, if I rewrite the call to sendRequest as below:
Code: datasource.sendRequest({ callback: { success: function(e) { Y.log(e.response.results.length + ' items returned', 'info', 'app'); }, failure: function(e) { Y.log(e.error.message, 'error', 'app'); } } }); I am able to access the object values in the JSON response. The problem here is: I do need to attach request parameters in order to filter my data. Something like this: Code: datasource.sendRequest({ request: 'param1=' + value1 + '¶m2=' + value2, callback: { success: function(e) { Y.log(e.response.results.length + ' items returned', 'info', 'app'); }, failure: function(e) { Y.log(e.error.message, 'error', 'app'); } } }); This, however, raises the same "Unexpected token :" error, even if the parameterized response equals the nonparameterized one. What did I get wrong here? I appreciate any help. Thanks, Leonardo |
Leonardo Martins
|
Okay, here's more information. I have noticed that when the request attribute is not specified, my GET request is formed like this:
Code: "GET /api/v0/test/?format=jsonundefined&callback=YUI.Env.DataSource.callbacks.yui_3_3_0_1_1330367111332_661 HTTP/1.1" The undefined string added to the GET request interestingly prevents the error. If, on the other hand, I add an empty string to the request attribute, like this: Code: datasource.sendRequest({ request: '', //empty string callback: { success: function(e) { Y.log(e.response.results.length + ' items returned', 'info', 'app'); }, failure: function(e) { Y.log(e.error.message, 'error', 'app'); } } }); so that Code: "GET /api/v0/operation/?format=json&callback=YUI.Env.DataSource.callbacks.yui_3_3_0_1_1330367111332_658 HTTP/1.1" then I get the same "Unexpected token :" error. Thanks, Leonardo |
Leonardo Martins
|
PROBLEM SOLVED!
This is for other beginners out there: use DataSource.IO instead of DataSource.Get if you don't want your JSON data wrapped in a callback function call. So, your instantiation code should look like this: Code: var datasource = new Y.DataSource.IO({ source: 'http://127.0.0.1:8000/api/v1/test/?format=json&' }); |
| Page 1 of 1 | [ 8 posts ] |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group