Cross-Domain get CSV file - javascript

I can't seem to get a CSV stock file from yahoo finance to "success"-fully load. I've tested different callbacks and suggestions from other questions, but none of them seem to be working - most of them don't output anything.
$(document).ready(function(){
$.ajax({
url:"http://finance.yahoo.com/d/quotes.csv?s=XOM&f=sn",
dataType: 'jsonp',
success: function(data) {
alert('good');
},
error: function(data) {
alert(data);
}
});
});
​This code alerts [object Object] (the "error" callback), however the CSV file can be seen in the network panel successfully. The data in the network panel reads "XOM, Exxon Mobile Corpo" as expected (so it did load).
I guess the real question is how can I get that data which I know is loaded. I just want to alert it for now... just want to see it on the page. I've spent a countless number of hours fiddling with this and it just doesn't work.
Here's a jsfiddle:
http://jsfiddle.net/V94sQ/3/

You can not request CSV files from another domain unless they support CORS. Since you do not control yahoo you are out of luck there. You would need to use a proxy [request it from your own server, backend makes the get request] or a service that can make it into a jsonp request.

Related

AJAX post url returning 400 (bad request)

I'm using some simple AJAX page transition on a Craft CMS site, very similar to these instructions: https://designbycosmic.com/journal/craft-cms-ajax-page-transitions-with-history-pushstate
The basic structure of my code is this:
$.ajax({
type: 'POST',
url: href,
data: {},
success: function(result){
// hide old content, load new content, fade in new content
},
error: function(){
console.log("ajax error");
}
});
"href" is the link to new page to load.
I'm always getting a 400 (Bad Request) error.
I'm using this exact same method for a similar site on the exact same hosting environment and it works fine, so I'm banging my head as to why this would be acting differently.
All of the other answers on this subject seem to be for when "data" is not formatted correctly, but none address the issue when you're simply loading a URL, with no other data.
In this case, what would cause a 400 error?
UPDATE
OK here's the live site for anyone who is kind enough to help further investigate: http://mearch.nz/
Still very incomplete and under development. You should be able to navigate between Home, Projects, and and then any project detail page linked from the Projects index. You'll see that the loading indicator just gets stuck there and the new page never arrives because the AJAX fails.
If you reload any of those pages they work fine. Nothing wrong with the URL. For some reason it's just not accepted as proper data for the AJAX POST.
Now for comparison, here's another website on the same web host, using the same AJAX functions, and same CMS, and it works fine: http://benek.nz/
http:// mearch.nz/projects/ does not reference http:// mearch.nz/js/init.js. But init.js needs to be loaded since it contains your javascript-code!
In contrast http://benek.nz does reference init.js and therefore it works.
Looks like you use minmee on http:// mearch.nz and therefore it is a bit difficult to debug your issue.
It seems that your request has an CSRF-issue. This normally happens if javascript sends a request to a web-server, which is different to where the javascript comes from.
This is what I did to reproduce your issue:
navigated to http://mearch.nz/projects/
clicked on link "projects"
In Google Chrome: the console window writes "... 400 (Bad Request)"
In Google Chrome in the network tab under response I see the server also sent HTML containin text "Bad Request" and "The CSRF token could not be verified."

How to download silently with javascript

I am new in javascript and I have some trouble to complete an activity...
I need working, in the client side, with the information uploaded in a "special" server like this:
http://www.uninorte.edu.co/documents/71051/11558879/ExampleData.json/0a635cdd-ccdd-4a1c-8c88-b53bea431458
I want load it in main memory, without the browser show the explicit download.
I try to use some solutions, but really I have no idea how to proceed to achieve it.
... Beforehand thank you very much
(I am not a native english speaker, I apologize if I do not write well)
[Solved]
I decided, for the moment, use the Whatever Origin services, that returns me something I can read with $.getJSON "Without download" the file.
Resulted:
<script type="text/javascript">
$.getJSON('http://whateverorigin.org/get?url=' + "http://www.uninorte.edu.co/documents/71051/11558879/ExampleData.json/0a635cdd-ccdd-4a1c-8c88-b53bea431458" + '&callback=?', function(data){
alert(data.contents);
});
</script>
Thank you for yours responses, really you gave me lights in order to solve it
Regarding silent part
Your browser is always aware of the network requests which are made from JS. Therefore, the user can always see all the requests and responses by opening developer tools.
Now coming to loading a remote json to the memory in the client
Since you mentioned you are relatively a newbie in JS, I am going to cover the very basic, so please bear with me if you already know it
You need to make an ajax call using an XMLHttpRequest as shown here
However, most people use some library like jQuery while working to abstract checking state of the request and other trivial tasks. This results in making an ajax call as simple as calling a method and providing a callback method to process the response.
$.ajax({
url: '/path/to/file',
type: 'default GET (Other values: POST)',
dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
data: {param1: 'value1'},
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
You may find the example at below link.
http://api.jquery.com/jquery.getjson/
P.S.: Due the less reputation points, I can neither post any supporting images nor more than two links.

JSONP request callback

Ok, after an whole day trying to get this to work I just don't seem to know what's the deal with this, so here it goes:
I have a WebService hosted locally by now (http://localhost:15021/Service1.svc/[whatever_method]) and an HTML Page on a different file.
FYI, both WebService and HTML Page will get hosted on different servers.
I'm trying to get some info off the WebService to the HTML Page by the onload=load() method in the HTML Page.
My JavaScript code is:
function load() {
alert("Loading...");
$.ajax({
url: 'http://localhost:15021/Service1.svc/getAllNoticias',
type:'GET',
dataType: 'jsonp',
jsonp: 'loadNews_callBack'
});
}
function loadNews_callBack(result){
alert(result.data);
}
Additionally, the JSONP is loaded on the Page with an OK (200) status (As you can see here) which should (I guess) call the callback function, but it isn't.
What can I do to get around this? I already change the request parameters 500 times (e.g., added "?callback=? to the url, with or without jsonp attribute, etc...)
Any help would be great,
Thanks in Advance
Check the actual response text to verify that the response is correctly being wrapped by the callback function. It is possible that your service is not setup correctly to handle JSONP so it simply responding with JSON. It will still come back as 200, but fail to execute.
loadNews_callBack([json...])
vs
[json...]
See this question as reference on how to go about updating your service:
ASP.net MVC returning JSONP

how to get google results source code with jquery ajax

I want to get some google search results in my website,I know I can get with curl,php but its limited daily for same ip adress. and I dont want to use google search api because its also has limit. So I think I can get with jquery ajax but I m a bit new on that,I am fed up with this problem.
here is my code, its will be always error because of jsonp format, but maybe still there is a way for catch html source code. I see source code comes to my browser but I cant take it like object.I tryed xhr.responseText etc but its gives also SyntaxError, still I cant get.
if you can suggest to me any other ways or if you have any idea with below code please share with me.
Thanks before now
$.ajax({
url:"http://www.google.com.tr/search?q=ercan",
dataType: 'jsonp',
success:function(json){
// I know its wont never succes, because google gives source in html format
alert("Success");
},
error:function(xhr){
//I want to get source code html here, but its giving always parse end syntax error I cant get it
console.log(xhr);
},
});
I am afraid that your only choices are to use the API or server side bridge script. You cannot do cross domain AJAX calls if the server doesn't support JSONP or CORS. There's also a commercial version of the API which allows you to increase the limit of requests you could send.

How to debug JSONP AJAX event

I'm trying to get a JSONP AJAX request to go through, but I'm having problems figuring out why it's not working. Right now I have this call
$.getJSON(server_path+"formproxy.php?"+$(form).serialize()+"&action="+form.action+"&callback=?", function(data, status, xhr) {
alert(data);
});
but when I run the script nothing seems to happen. The best I can figure is that the jsonp request is running into an error, but because it's a jsonp request it's not actually reporting the error, which is greatly hindering my debugging.
I've played around with
$.ajaxSetup({
"error":function() {
alert("error");
}
});
and
$(document).ajaxError(function(e, xhr, settings, exception) {
alert('failed');
});
but I can't get either of them to trigger.
I've output the target url to the console, namely
server_path+"formproxy.php?"+$(form).serialize()+"&action="+form.action+"&callback=?"
I can visit the outputted url and see that it is working and outputs ?({"result":"success"}) which seems right to me.
Looking at the console in Chrome, it doesn't even show the XHR, but I'm at least sure the $.getJSON() code is being reached thanks to breakpoints.
I should finally note that I'm not even concerned with getting data back from the AJAX call. I just need to send form data cross domain and let the formproxy.php script process it.
Any thoughts on why this may not be working or techniques to help me see what's happening inside would be much appreciated.
Have you tried looking at the Network Panel in Chrome developer tools? It will show you all the http requests/responses that are being made on your page. That way you can tell for sure whether the the getJSON call is making a request to the server, and what the server response is (if any).
You can also use the Net panel in Firebug for this.

Categories