Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-02-2012, 03:36 PM   PM User | #1
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
felgall's cross-domain AJAX call

so, I'm looking at this:
http://javascriptexample.net/ajax07.php

The script attaches to the body like it should, but I can't figure out how to access the data. I understand that there should be some sort of jsonRequest function call, but I'm completely stumped as to what it would look like. here's my lame attempt thus far:

Code:
<body> 
<select id="addoptions"></select>
<script type="text/javascript">
jsonpRequest = function(url, callback) {
  var jnum, jname, scr;
  if (!jsonRequest.cnt) jsonRequest.cnt = 0;
  jnum = 'j'+ jsonRequest.cnt++;
  jname = 'jsonRequest.'+jnum;
  if (-1 === url.indexOf('?')) url += '?jsonp='+jname;
  else url = '&jsonp='+jname;
  scr = document.createElement('script');
  jsonRequest[jnum] = function(resp) {
     try{
        callback(resp);
        }
     finally{
        delete jsonRequest[jnum];
        scr.parentNode.removeChild(scr);
        }
  };
  scr.src = url;
  document.getElementsByTagName('body')[0].appendChild(scr);
}; 
 
jsonpRequest("http://qa-find.uglii.com/Home/GetCountries/9a3d0a019-e523-487f-8250-fd98c31b6a84.json",function(result){
for (var x = 0; x < result.length; x++) {
   document.getElementById("addoptions").options[x]=new Option(result[x].i,result[x].n);
      } 
});
</script>
</body>

Last edited by xelawho; 06-03-2012 at 05:52 AM..
xelawho is offline   Reply With Quote
Old 06-02-2012, 09:40 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
even if you corrected the obvious typo's
in that code it still will not work for you
unless you change the file found at
http://qa-find.uglii.com/Home/GetCou...8c31b6a84.json
or place some server side
code at that url.
DaveyErwin is offline   Reply With Quote
Old 06-02-2012, 10:08 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,468
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
The data is passed as an object directly into whatever function you specify as the callback function.

So if you have

jsonRequest('http://example.com', somefunc)

and

somefunc = function(x) {...}

the returned data is the x object passed to that function
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 06-03-2012 at 08:38 PM..
felgall is offline   Reply With Quote
Old 06-02-2012, 11:48 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
I must be missing something really simple, then, because all I get is :
Uncaught ReferenceError: jsonRequest is not defined
xelawho is offline   Reply With Quote
Old 06-03-2012, 12:31 AM   PM User | #5
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by xelawho View Post
I must be missing something really simple, then, because all I get is :
Uncaught ReferenceError: jsonRequest is not defined
Yes, jsonRequest is undefined in that code.
DaveyErwin is offline   Reply With Quote
Old 06-03-2012, 01:36 AM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Quote:
Originally Posted by DaveyErwin View Post
Yes, jsonRequest is undefined in that code.
Clearly. So I thought that, like you said, it must just be typo's and that changing all the references from jsonRequest to jsonpRequest might do something, but no dice - no error, but the callback function doesn't even run.

so...
xelawho is offline   Reply With Quote
Old 06-03-2012, 04:01 AM   PM User | #7
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by xelawho View Post
Clearly. So I thought that, like you said, it must just be typo's and that changing all the references from jsonRequest to jsonpRequest might do something, but no dice - no error, but the callback function doesn't even run.

so...
here is another typo ...
else url = '&jsonp='+jname;
Quote:
Originally Posted by DaveyErwin View Post
even if you corrected the obvious typo's
in that code it still will not work for you
unless you change the file found at
http://qa-find.uglii.com/Home/GetCou...8c31b6a84.json
or place some server side
code at that url.
DaveyErwin is offline   Reply With Quote
Old 06-03-2012, 04:15 AM   PM User | #8
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
I don't really know enough about what I'm doing to spot the errors - is that line supposed to be else url += '&jsonp='+jname;?

but your quoted comment confuses me - isn't the point of jsonp meant to be that you can do this kind of thing without having to have access the target document?

But maybe you're right, and that it is a problem with the file - the other jsonp examples out there seem to work fine on other files but not on this one. The closest I got was with the yahoo yql, which I was hoping to avoid as it seems to be just adding another step in the process, but at least it gets a result...

Code:
<body> 
<select id="addoptions" onchange="alert(this.value)"></select>
<script type="text/javascript">

function makeList(o){
result=o.query.results.json.json
for (var x = 0; x < result.length; x++) {
   document.getElementById("addoptions").options[x]=new Option(result[x].n,result[x].i);
      } 
document.getElementsByTagName('body')[0].removeChild(scr); 	  
}

var myurl=encodeURIComponent("http://qa-find.uglii.com/Home/GetCountries/9a3d0a019-e523-487f-8250-fd98c31b6a84.json")
var filetype="json"
var sel="*"

var scr=document.createElement("script");
scr.type="text/javascript"
scr.src='http://query.yahooapis.com/v1/public/yql?q=select%20'+sel+'%20from%20'+filetype+'%20where%20url%3D%22'+myurl+'%2F%22&format=json&callback=makeList';
document.getElementsByTagName('body')[0].appendChild(scr); 
</script>
</body>
xelawho is offline   Reply With Quote
Old 06-03-2012, 04:23 AM   PM User | #9
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by xelawho View Post
I don't really know enough about what I'm doing to spot the errors - is that line supposed to be else url += '&jsonp='+jname;?

but your quoted comment confuses me -
I'm confused by which comments
I have quoted ?
DaveyErwin is offline   Reply With Quote
Old 06-03-2012, 04:26 AM   PM User | #10
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
these ones:

Quote:
Originally Posted by DaveyErwin View Post
even if you corrected the obvious typo's
in that code it still will not work for you
unless you change the file found at
http://qa-find.uglii.com/Home/GetCou...8c31b6a84.json
or place some server side
code at that url.
xelawho is offline   Reply With Quote
Old 06-03-2012, 04:38 AM   PM User | #11
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by xelawho View Post
these ones:
Quote:
Originally Posted by DaveyErwin View Post
even if you corrected the obvious typo's
in that code it still will not work for you
unless you change the file found at
http://qa-find.uglii.com/Home/GetCou...8c31b6a84.json
or place some server side
code at that url.
yes same problem here ...

http://www.codingforums.com/showthread.php?t=262806
DaveyErwin is offline   Reply With Quote
Old 06-03-2012, 05:52 AM   PM User | #12
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
but wait...

if it works when hosted on my server:
jQuery
Code:
<script type="text/javascript">
$(document).ready(function(){
      $.getJSON("9a3d0a019-e523-487f-8250-fd98c31b6a84.json",function(result){
      $.each(result, function(i){
	   $("#addoptions").append('<option name='+result[i].i+'style="padding: 10px 0pt 0pt; float: left;">'+result[i].n+'</option>');
      });
    });
});
</script>
vanilla:
Code:
function downloadUrl(callback) {
url="9a3d0a019-e523-487f-8250-fd98c31b6a84.json"
var jsonFile = window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject('Microsoft.XMLHTTP');
	jsonFile.onreadystatechange = function() {
   if (jsonFile.readyState == 4) {
     callback(jsonFile, jsonFile.status);
   }
 };
jsonFile.open("GET", url, true); 
jsonFile.send(null);
}


downloadUrl(function(data) {
result=JSON.parse(data.responseText);
  for (var x = 0; x < result.length; x++) {
   document.getElementById("addoptions").options[x]=new Option(result[x].n,result[x].i);
      } 
  });
shouldn't jsonp be able to do the same thing cross-domain?
xelawho is offline   Reply With Quote
Old 06-03-2012, 10:25 PM   PM User | #13
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
the code in post #1 is completely
different from the code in post #12
DaveyErwin is offline   Reply With Quote
Old 06-03-2012, 10:54 PM   PM User | #14
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
yeah - you should have seen all the variations in between

but this is kind of my point: works same-domain (post #12) doesn't work cross-domain (post#1)

I've pretty much given up, anyway... the code in 1 works on other json files (once the typo's are corrected), so I guess it's just something about that one
xelawho is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:26 AM.


Advertisement
Log in to turn off these ads.