-1

Here is a partial of the JSON i'm trying to access via cross domain ajax request:

{
        "request": "Stream/GetDigest",
        "response": {
            "success": true,
            "content": {
                "0": {
                    "artifact_id": "36",
                    "timestamp": "2013-08-20 11:59:00",
                    "modified": "2013-08-20 11:59:00",
                    "text": "Why did the last one duplicate? I don't think I clicked it twice...",
                    "author_desc": "",
                    "object_type": "artifact",
                    "comments": []
                    },

and so on....

Here is my ajax function:

            loadDigest: function() {
                $.ajax({
                    crossDomain: true,
                    type: "GET",
                    dataType: "jsonp",
                    crossDomain: true,
                    async: true,
                    cache: false,
                    url: 'http://DOMAIN_NAME/Stream/GetDigest?digest_hash=43c4901481f7f1acd825537aa91a7dd36561d30e',
                    success: function(data) {

                        var source      = $('#artifact_list').html(),
                            template    = Handlebars.compile(source),
                            html        = template(data.response);

                            $content_area.html(html);

                    },
                    error: function(error) {
                        $content_area.html('<strong>There was an error: ' + error + '</strong>');
                    },
                    timeout: default_timeout,
                    beforeSend: object_spinner, // calling variable for default loading graphic
                    complete: function() {
                        $content_area.removeClass('is_getting');
                    },
                    jsonp: 'jsonp-callback'
                })

I'm getting a 200 OK status code, however in the console I can't seem to get anything other than "Uncaught SyntaxError: Unexpected token :" on the "GetDigest:1".

I've tried a few different methods i've found on StackOverflow and other places, but can't seem to get this running. Any help would be appreciated.

Thanks!

flashpunk
  • 772
  • 2
  • 13
  • 38
  • is there a `response.content[1]` in your json data? what is it? – collapsar Aug 21 '13 at 14:55
  • `JSON != JSONP` Also, MANY of the parameters you are passing to $.ajax are completely ignored due to the fact that it's a jsonp request. – Kevin B Aug 21 '13 at 14:57
  • @collapsar, it looks much like the response.content[0] - different ID, and content. not sure what this should matter? – flashpunk Aug 21 '13 at 15:00
  • @KevinB - yeah, i've been trying a bunch of stuff, so there is some extra stuff there. Will have to clean it up once it's working. Any suggestions on how I can make this work? – flashpunk Aug 21 '13 at 15:01
  • @flashpunk: the error message you receive suggests that the cause of the error reside there. – collapsar Aug 21 '13 at 15:02
  • @flashpunk Yes, modify your service so that it returns JSONP and not JSON. (read, JSON with Padding) – Kevin B Aug 21 '13 at 15:02
  • Think of JSONP as nothing more than including a script that executes a function. For example, this is a valid example of JSONP: `jsonpCallback298347019346017846091827350817({"foo":"bar"})` It gets around the same-origin policy due to the fact that the same-origin policy doesn't apply to script includes. – Kevin B Aug 21 '13 at 15:03
  • Just noticed that on the request: line (in the raw json), the value is "request": "Stream\/GetDigest" - I think the "\/" might be the issue. – flashpunk Aug 21 '13 at 15:07
  • that doesn't quite match the error you're getting. – Kevin B Aug 21 '13 at 15:14
  • Is there a way I can debug the json data? I've put it through jsonlint.com, and it says there are no errors. – flashpunk Aug 21 '13 at 15:19
  • If you put the json data exactly as it is being recieved directly into jsonlint.com and it said it was valid, then your json is NOT valid JSONP. JSON and JSONP are two different datatypes. `{"foo":"bar"}` is valid JSON, `callback({"foo":"bar"})` is valid JSONP (assuming the client is expecting a callback named callback) – Kevin B Aug 21 '13 at 15:43
  • Right, ok thank you @KevinB I've checked the JSONP in a json-p validator and it says it is malformed. So it looks like that if I want to do this cross-domain, that I need some kind of proxy. – flashpunk Aug 21 '13 at 15:48
  • Do you not have control of the server that the service is on? there is a freely available public proxy that you can use to retrieve this kind of data (YQL), that or you can build your own on your server. – Kevin B Aug 21 '13 at 15:49
  • I don't currently. Right now this is just for testing purposes. Was hoping to get it running, but It doesn't look like it's going to happen. Thank you for your assistance @KevinB – flashpunk Aug 21 '13 at 18:17
  • **I wrote an answer related to cross domain requests here: [Loading cross domain html page with jQuery AJAX](http://stackoverflow.com/questions/15005500/loading-cross-domain-html-page-with-jquery-ajax/17299796#17299796)** – _the last one, supports https_ – jherax Jun 26 '14 at 16:56

1 Answers1

0

This is a cross domain json issue.

flashpunk
  • 772
  • 2
  • 13
  • 38