0

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.

6
  • are you getting any errors? Any data? Have you checked using firebug? Is this a same-origin-policy restriction? en.wikipedia.org/wiki/Same_origin_policy Commented Oct 2, 2013 at 6:12
  • 2
    The URL returns JSON, not JSONP, so you cannot use JSONP. Since it is an external domain, you cannot use Ajax, unless the server supports CORS. Commented Oct 2, 2013 at 6:19
  • its htmlrequest and sameoriginpolicy restriction, but i think i already did try everything and i still can't make it to work Commented Oct 2, 2013 at 6:21
  • @FelixKling If its json it should work on my first script right? since my first script reads a saved json file that is exactly the link's content Commented Oct 2, 2013 at 6:23
  • 1
    No, it doesn't because you are accessing an external domain. You cannot load any data from an external domain via Ajax unless it has CORS enabled. JSONP is actually not Ajax, it works by dynamically adding a <script> element. Commented Oct 2, 2013 at 6:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.