jquery - How to fix Access-Control-Allow-Origin error from a remote site -
i'm trying use service @ http://rate-exchange.appspot.com/currency, the access-control-allow-origin error.
understanding server problem, , http://rate-exchange.appspot.com/currency not service, have no idea how fix problem.
using service this:
$.getjson("http://rate-exchange.appspot.com/currency", { "from": currency.from, "to": currency.to }, function (result) { if (!result.err) { currency.rate = result.rate; $("#footer-output").text("all sales in " + currency.to + " including vat"); } else { $("#footer-output").text("all sales in local currency including vat"); } });
now trying data directly in browser not problem. there can do, or can fixed on server?
the following work. took liberty of adding currency object code. part defining elsewhere.
plunkr: http://beta.plnkr.co/amvpve9kuppcsd0ma5s3
code:
$(function(){ currency = {}; currency.from = "usd"; currency.to = "pen"; currency.rate= 0; $.getjson("http://rate-exchange.appspot.com/currency?callback=?", { "from": currency.from, "to": currency.to }, function (result) { if (!result.err) { currency.rate = result.rate; $("#footer-output").text("all sales in " + currency.to + " including vat"); } else { $("#footer-output").text("all sales in local currency including vat"); } });
adding callback=? url , jquery issue jsonp request. here section docs (halfway down): http://api.jquery.com/jquery.getjson/
jsonp
if url includes string "callback=?" (or similar, defined server-side api), request treated jsonp instead. see discussion of jsonp data type in $.ajax() more details.
Comments
Post a Comment