Skip to content

Instantly share code, notes, and snippets.

@bsodmike
Created September 1, 2011 15:12
Show Gist options
  • Save bsodmike/1186379 to your computer and use it in GitHub Desktop.
Save bsodmike/1186379 to your computer and use it in GitHub Desktop.
SOP Failing Yahoo stock quotes via AJAX
<!doctype html>
<html>
<head>
</head>
<body>
<h2>result</h2>
<div class="result"></div>
<h2>result2</h2>
<div class="result2"></div>
<!--SCRIPTS-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">//<![CDATA[
/* Usage:
* jQuery.csv()(csvtext) returns an array of arrays representing the CSV text.
* jQuery.csv("\t")(tsvtext) uses Tab as a delimiter (comma is the default)
* jQuery.csv("\t", "'")(tsvtext) uses a single quote as the quote character instead of double quotes
* jQuery.csv("\t", "'\"")(tsvtext) uses single & double quotes as the quote character
*/
jQuery.extend({
csv: function(delim, quote, linedelim) {
delim = typeof delim == "string" ? new RegExp( "[" + (delim || "," ) + "]" ) : typeof delim == "undefined" ? "," : delim;
quote = typeof quote == "string" ? new RegExp("^[" + (quote || '"' ) + "]" ) : typeof quote == "undefined" ? '"' : quote;
lined = typeof lined == "string" ? new RegExp( "[" + (lined || "\r\n") + "]+") : typeof lined == "undefined" ? "\r\n" : lined;
function splitline (v) {
// Split the line using the delimitor
var arr = v.split(delim),
out = [], q;
for (var i=0, l=arr.length; i<l; i++) {
if (q = arr[i].match(quote)) {
for (j=i; j<l; j++) {
if (arr[j].charAt(arr[j].length-1) == q[0]) { break; }
}
var s = arr.slice(i,j+1).join(delim);
out.push(s.substr(1,s.length-2));
i = j;
}
else { out.push(arr[i]); }
}
return out;
}
return function(text) {
var lines = text.split(lined);
for (var i=0, l=lines.length; i<l; i++) {
lines[i] = splitline(lines[i]);
}
return lines;
};
}
});
//]]></script>
</script>
<script type="text/javascript">
$(document).ready(function(){
// working ajax example
// var jqxhr = $.ajax({
// type: "GET",
// url: "http://twitter.com/users/usejquery.json?callback=?",
// dataType: "json",
// success: function(json_data) {
// $(".result").html(json_data.status.text);
// },
// error: function(xhr, textStatus, errorThrown) {
// $(".result").html("Err:" + xhr.responseText);
// }
// });
$.get("http://finance.yahoo.com/d/quotes.csv?s=XOM+BBDb.TO+JNJ+MSFT&f=snd1l1yr", function(d2){
alert("Data Loaded: " + d2);
})
.error(function(xhr, textStatus, errorThrown) {
$(".result2").html("Err:" + xhr.responseText + textStatus + errorThrown);
});
var stock = $.ajax({
type:"GET",
url: "http://finance.yahoo.com/d/quotes.csv?s=XOM+BBDb.TO+JNJ+MSFT&f=snd1l1yr",
success: function(data){
array = jQuery.csv()(data);
alert(array);
},
error: function(xhr, textStatus, errorThrown) {
$(".result").html("Err:" + xhr.responseText + textStatus + errorThrown);
}
});
});
</script>
</body>
</html>
@bsodmike
Copy link
Author

bsodmike commented Sep 1, 2011

Same origin policy prevents a document or script loaded from one origin from getting or setting properties of a document from another origin. Two pages are considered to have the same origin if the protocol, port, and host are the same for both pages. http://rj3.net/mdc/sop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment