Quantum
Quantum

Reputation: 1476

jsonp, just not working.. always error

I have read and read and read.. I am trying to pull a feed I have followed ever last answer i can google. I don't get what the issues are here cause firebug is fine with finding and parsing the darn json.

so try one

var url='http://images.wsu.edu/javascripts/tracking/configs/pick.asp';
$.getJSON(url+'?callback=?', function(data){
    alert('true');
    $.jtrack({
        load_analytics:{account:GAcode},
        trackevents:data // this can be hard codded here or set do be feed in like so
        });
});

FAIL yet i see it in WTF?

so

var url='http://images.wsu.edu/javascripts/tracking/configs/pick.asp';
$.ajax({ 
  url: url, 
  cache: false,  
  dataType: 'jsonp',
  jsonp : "callback",
  success: function(data){ 
    alert('data');
    //data=jQuery.parseJSON(data);
    $.jtrack({
        load_analytics:{account:GAcode},
        trackevents:data // this can be hard codded here or set do be feed in like so
    });
  }, 
  error: function(e, xhr){ 
    alert('error');
    alert(e);
  } 
});

And again FAIL .. so.. um think error would tell me yet.. nothing.. so.. lets be real and test the output it's self cause sure as hell it finds the url and I'm invoking jsop and and and.. well lets validate the output cause surely that is the issue.

enter image description here

So.. well I don't know.. I have hit every darn thread here from bangkok. One one know what the deal is?

Upvotes: 0

Views: 1168

Answers (4)

Mark Rhodes
Mark Rhodes

Reputation: 10217

I think the problem is that you are not returning JSONP content - just JSON. You need to prepend the callback parameter to the start of the JSON you are currently serving and also wrap it in parenthesis. e.g. you are currently returning:

[{
    "element":"#null",
    "options":{}
 }, 
   ...
]

and you need to be returning:

value_of_callback_param([{
    "element":"#null",
    "options":{}
  }, 
  ...
]);

The trailing ; is optional. See this link which describes how it's supposed to work.

In addition I think application/javascript should be used for the content type instead of application/json, since you will be returning valid JavaScript instead of just a JSON object/array. Also, the jQuery docs for the $.ajax function states that the error callback should not called for JSONP requests.

Upvotes: 0

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

this alerts "#null"

var url = 'http://images.wsu.edu/javascripts/tracking/configs/pick.asp';
$.getJSON(url + '?callback=?', function(data) {
  alert(data[0].element);
});

here is what comes back:

callback([
    {
        "element":"#null",
        "options":{}
    },{
        "element":"a#hover",
        "options":{
            "category":"hover",
            "label":"function(ele){ return ( ($(ele).attr('title')!='' && typeof($(ele).attr('title')) !=='undefined' ) ? $(ele).attr('title') : $(ele).attr('href') ) }"
        }
    },{
        "element":"a[href$='.jpg'],a[href$='.gif'],a[href$='.png'],a[href$='.ppt'],a[href$='.pdf'],a[href$='.doc'],a[href$='.docx']",
        "options":{
            "category":"download",
            "label":"function(ele){ return ( ($(ele).attr('title')!='' && typeof($(ele).attr('title')) !=='undefined' ) ? $(ele).attr('title') : $(ele).attr('href') ) }"
            }
    },{
        "element":"a:not([href*='mailto('])",
        "options":{
            "category":"email"
            }
    },{
        "element":"a:not([href*='wsu.edu'])",
        "options":{
            "category":"outbound"
            }
    },{
        "element":"a[href*='wsu.edu']",
        "options":{
                "category":"internal",
                "noninteraction":"true"
            }
    }
])

Upvotes: 0

stewe
stewe

Reputation: 42632

Just make pick.asp get the value of the callback url parameter (called <some string> from now on), which gets generated by jquery and sent like this pick.asp?callback=<some string> to the server. Then use that value to build your response like this:

<some string>(<your json>);

Upvotes: 0

danfolkes
danfolkes

Reputation: 414

Checkout this link: JSON Example using Callback

I think the JSON returned needs to put the callback in the returned JSON. In the example above, it's placed at the front like:

CALLBACKVALUE([{"id":"4","name":"FICTION","parentid":"3","getfullname":"BOOKS > FICTION"},{"id":"5","name":"NON-FICTION","parentid":"3","getfullname":"BOOKS > NON-FICTION"})

Upvotes: 1

Related Questions