A Super Awesome
A Super Awesome

Reputation: 93

JSONP from URL returns nothing

Hi ive been reading for almost 1week about json.

$.getJSON( 'accounts.json', function(data ) {
$('#try1').append("<option value='0' name='idsel'>Select Outlet</option>");
console.log(data);
$.each(data, function (i, item) {
$('#try1').append("<option value='" + item.outlet_group_id + "'>" + item.outlet_group_name + " : " + item.outlet_group_code + "</option>");
});
});

i have this script that works fine, but when i load it on the url it doesnt appear the url is this http://nutri.de.imerchmedia.com/services/accounts

Also i have tried jsonp, i came up with this

$.ajax({
url: 'http://nutri.de.imerchmedia.com/services/accounts',
dataType: 'jsonp',
success: function(data1){
$.each(data, function (k,v) {
console.log(data1);
$('#form').append(k + ':' + v);
});
}
});

i wonder is it me or it is the host?

any help would be much appreciated. also feel free to try extracting the data thru the link.

Upvotes: 0

Views: 92

Answers (2)

aaronfay
aaronfay

Reputation: 1692

Looking at that URL you provided, there is no callback wrapper for JSONP to work. JSONP requires the output of a regular JSON data structure to be "wrapped" in a function on your page. Normally jQuery does this for you, so you would call your service with:

http://nutri.de.imerchmedia.com/services/accounts?callback=foobar

and you would be expected to have a function foobar (data) {...} on the page. Again, jQuery creates this function for you, but your service would need to wrap the response data like so:

foobar('jsontoreturnhere...')

However

Given that data and your example code, I have a copy of it working here: http://jsfiddle.net/aaronfay/jCqwp/

I cannot do the Ajax call on jsfiddle, so I'm only simulating the internal part of your function. I suspect that is doing what you want, so I think it's probably either:

  • your #try1 selector is wrong/misspelled
  • make sure you are using a <select id='try1'>

gl
Aaron

Upvotes: 0

Patrick Gunderson
Patrick Gunderson

Reputation: 3281

This is most likely a Cross-Origin issue. You can't load json into your website from another domain without their permission. Their server must add a Cross-Origin-Allow header to the file it sends you that tells the browser it's ok. It doesn't look like http://nutri.de.imerchmedia.com/services/accounts is sending that header. So you won't be able to use it.

Jsonp is a format that people are using to get around the json cross origin restrictions. Whether or not a server will send jsonp or just json is dependent on the server configuration and it doesn't look like imerchmedia has taken the steps to enable serving jsonp.

Upvotes: 2

Related Questions