[ 8 posts ]

Leonardo Martins

  • Offline
  • Profile
Tags:

"Unexpected token :" error using DataSource.Get

Post Posted: Fri Feb 24, 2012 7:43 am
+0-
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

Dav Glass

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 2088
  • Location: Marion, IL, US
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • Offline
  • Profile

Re: "Unexpected token :" error using DataSource.Get

Post Posted: Fri Feb 24, 2012 7:51 am
+0-
It looks like your JSON response may be invalid, what is the output from that url?

Leonardo Martins

  • Offline
  • Profile

Re: "Unexpected token :" error using DataSource.Get

Post Posted: Fri Feb 24, 2012 8:17 am
+0-
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": ""}]}

Dav Glass

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 2088
  • Location: Marion, IL, US
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • Offline
  • Profile

Re: "Unexpected token :" error using DataSource.Get

Post Posted: Fri Feb 24, 2012 8:28 am
+0-
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

  • Offline
  • Profile

Re: "Unexpected token :" error using DataSource.Get

Post Posted: Fri Feb 24, 2012 9:01 am
+0-
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

  • Offline
  • Profile

Re: "Unexpected token :" error using DataSource.Get

Post Posted: Mon Feb 27, 2012 10:08 am
+0-
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 + '&param2=' + 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

  • Offline
  • Profile

Re: "Unexpected token :" error using DataSource.Get

Post Posted: Mon Feb 27, 2012 10:47 am
+0-
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

  • Offline
  • Profile

Re: "Unexpected token :" error using DataSource.Get

Post Posted: Mon Feb 27, 2012 11:46 am
+0-
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&'
});
  [ 8 posts ]
Display posts from previous:  Sort by  
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