I am trying to access a remote json file using YUI, example code can be seen in the jsfiddle below. The request goes off to the server (you can see it in Google dev tools network tab)
. The on success or failure functions don't get executed which I can't understand
http://jsfiddle.net/brendan_rice/4FZc4/3/
Can anyone help please?
Your datasource must support the callback syntax, wrapping the data with callback([...]);
Read the first section (in blue) http://yuilibrary.com/yui/docs/datasource/datasource-get.html
I figured out the issue by putting in YUI({ filter: 'debug' }), which showed that there was a syntax error in the .Get request (which is indicative of sending over unwrapped JSON data).
Also, if you just want the raw data from a cross-origin request and don't need a real DataSource instance, you may find Y.jsonp easier to use (http://yuilibrary.com/yui/docs/jsonp/).
Related
I am using python 2.7 on windows 7 with requests requests module.
I am trying to find the source of some data from a website that uses AJAX/javascript to drive events.
Here is my python post:
result = s.post(url, headers = {'referer': my_referer})
The output from this post looks like this:
{"ADATA":{"COUNT":0.0,"AITEM":[]},"WM":0.0,"CM":0.0,"PC":"","PW":"","NC":14,"RR":false,"RTIME":{"RITEM":[],"COUNT":0.0},"WC":1.0,"CC":1.0,"RW":false,"RA":false,"RC":true}
"AITEM":[] should be populated with all the data I'm after but as you see its not. Also RC: should be false. Or at least it is when I go through my browser
Note that I get "RA":true and "RC":false if I use get instead of post request. I dont know why.
Here is the corresponding post in the javascript server side:
$.getJSON (url, function(data){UpdateStuff(data);});
Disclaimer: I have been programming for about a week now.
This JSON post is in a function that receives no parameters and returns nothing as far as I can tell. data is not referenced in the function prior to the post. I dont really understand what function(data) is. UpdateStuff(data) is another function with a bunch of code that takes data and returns nothing. The code reveals some stuff about the data structure with references such as:
if (data.RA){resetAL();} else {
if (data.RR ){objAR.attr('ref','Y');}
if (data.RC ){objAC.attr('ref','Y');}
if (data.RW ){objAW.attr('ref','Y');}
Which I guess I am failing this logic gate since my data is different than what I see in a successful browser request.
and references to data.ADATA.COUNT and data.ADATA.AITEM[i] etc
I've been told this would be much simpler to write in JS but I have about 70% of this program done in python already and I've never used JS.
Any help is greatly appreciated.
I am doing a jquery.ajax() call on one of our pages to fetch a small text file. I see some of the requests (not all) fail with resp.statusText: "No Transport" and resp.status : 0
What does the error mean (No Transport with a resp code of 0). Strangely it works on some browsers, and doesn't work on some. I couldn't find a patter by looking at the user agents of browsers, where it failed.
Any help would be highly appreciated. I am a beginner to javascript and jquery library, let me know if I omitted crucial information.
My use case:
abc.mydomain.com contains jquery.ajax(url:xyz.mydomain.com) call
Most likely it prevents you from firing a request because it things you are trying to access another domain. xyz.mydomain.com !== mydomain.com.
Why that is not allowed?
Read
Use a Web Proxy for Cross-Domain XMLHttpRequest Calls
Why the cross-domain Ajax is a security concern?
An example to why this is a security issue, assume you installed a bad plugin to your browser. If that plugin got the permission, it can read all loaded files to your browser and be able to edit/change/inject content and codes. Then it might send all collected data to designer own server.
... The most common business needs that are easily accomplished with browser plug-ins are: modify default search, add side frames, inject new content into existing webpage ...more
A good practice is to fetch the data thru ajax via JSON, if you are trying to access another site beside the one the script is calling from, then use JSON-P.
Read
JSON-P
JSON-P call to subdomain
Chrome ajax call to subdomain
A common architecture is to call the current domain that the script is loaded from, then use server script to fetch data from the other domain where the other domain will response to the request and return the data.
A code snippets of your function will help us understand your issue more.
I am developing an application that needs to gather information from GitHub, so I began looking at their API. My initial thought was to use the d3.json() function to get the data (because it's simple and has done good things for me in the past), but there doesn't appear to be a way for me to authenticate with that function. For example, $ curl -u "username" https://api.github.com is given as an example of basic authentication (from GitHub API--obviously they use curl for their examples).
So, is there a way to do authentication with the d3.json() function? And if not, what are my other options to get the JSON with JavaScript?
Thanks!
Edit:
I'm experimenting now with using jQuery's getJSON method as shown here, because I started getting the error "XMLHttpRequest cannot load url Origin url is not allowed by Access-Control-Allow-Origin." Of course, the switch doesn't help with the ability to authenticate, but I can at least get the public data (which is most).
Edit #2:
Has anyone experimented with michael/github or fitzgen/github-api? I'm going to start looking into those.
If you have a PHP script which echos JSON, you can do all authentication server-side. Note that you can also send a GET request to your PHP script, so the way you call your script can be dynamic.
I have some problem with $.getJSON response in Chrome
The query is
$.getJSON("http://www.askgeo.com/api/428014/sf2t36ujv1tsf325t5734gstr4/timezone.json?callback=?&points=55.77184,37.623553",
function(json){
<some code>
}
);
if you click on this link you'll get an json text.
By when I run this query Chrome shows an error:
Resource interpreted as Script but transferred with MIME type application/json
SyntaxError: Unexpected token : timezone.json:1
Does it try to convert json response to JavaScript object? If it is so why it cann't do that? Is there any way of resolving this problem?
in Chrome debugger I found the file "timezone.json" with this content:
{"code":0,"message":"ok","data":[{"timeZone":"Europe/Moscow","currentOffsetMs":14400000,"latitude":55.77184,"longitude":37.623553}]}
The server you are requesting data from is not setup to return JSONP. therefore, you need to build some kind of proxy to get the data for you, or use YQL.
Edit:
If you were to use YQL, this is the url you would use:
http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20json%20WHERE%20url%3D%22http%3A%2F%2Fwww.askgeo.com%2Fapi%2F428014%2Fsf2t36ujv1tsf325t5734gstr4%2Ftimezone.json%3Fpoints%3D55.77184%2C37.623553%22&format=json&diagnostics=true
and for information on how I generated that url, visit:
http://developer.yahoo.com/yql/console/#h=SELECT%20*%20FROM%20json%20WHERE%20url%3D%22http%3A//www.askgeo.com/api/428014/sf2t36ujv1tsf325t5734gstr4/timezone.json%3Fpoints%3D55.77184%2C37.623553%22
You can find the url at the bottom.
Fiddle using YQL: http://jsfiddle.net/JGwU3/1/
there is however one quirk with using YQL. if the result only contains one result, it's contents is an object, however, if it is multiple, its contents will be an array. you can see the difference by console.logging the response.
In the API documentation it says, that you should provide the query paramaters in a separate JSON object as the second argument.
$.getJSON('http://www.askgeo.com/api/428014/sf2t36ujv1tsf325t5734gstr4/timezone.json', {'callback':'', 'points': '55.77184,37.623553'}, function(json) {
alert(json.data[0].timeZone);
});
Works fine when I tested it.
(This is totally ignoring JSONP)
EDIT
OK, my post was wrong, it is a JSONP issue. See this jQuery documentation page on how to retrieve the data from the URL:
http://api.jquery.com/jQuery.ajax/
Anyone, help me, how to get Headers and PostData inside extension, using xpcom/something else?
i cant find functions inside firebug due to it is big codebase... thank you guys
I am assuming that you want request headers, not response headers. Then you register an observer for the http-on-modify-request notification. The general documentation is https://developer.mozilla.org/en/Observer_Notifications#HTTP_requests, a code example can be found under https://developer.mozilla.org/en/Creating_Sandboxed_HTTP_Connections#Handling_cookies. Getting the headers is easy, you simply call nsIHttpChannel.getRequestHeader().
Getting response headers is similar but you need to listen to http-on-examine-response notification and probably http-on-examine-cached-response as well.
POST data is tricky. You can get the upload stream as httpChannel.QueryInterface(Components.interfaces.nsIUploadChannel).uploadStream. But the stream can only be read once - it is either you or the code sending data to the server. So you need to replace the stream by something that looks identical but allows you to peek on it - probably nsIInputStreamTee where you set the original stream as source and nsIPipe as sink. Not sure whether this is the approach chosen by Firebug.
Wladimir is right on for the headers. For the POST data, here's a couple of code snippets with a little more detail:
https://developer.mozilla.org/en/Code_snippets/Miscellaneous#Getting_postData_of_a_webpage