[英]Yahoo Unauthorized JSONP request with Django
I have template Test.html
: 我有模板
Test.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta name = "viewport" content = "width = device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script>
</head>
<body >
<h1>Stock Quotes AutoSuggest </h1>
<br><br>
<input style="width:800px; height:20px;" id="txtTicker" class="x"/>
<script type="text/javascript">
var YAHOO = {
Finance: {
SymbolSuggest: {}
}
};
$(".x").autocomplete({
source: function (request, response) {
var query=request.term;
$.ajax({
type: "GET",
url: "http://d.yimg.com/autoc.finance.yahoo.com/autoc",
data: {query: query,region:'US',lang:'en-US'},
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "YAHOO.Finance.SymbolSuggest.ssCallback",
});
// call back function
YAHOO.Finance.SymbolSuggest.ssCallback = function (data) {
var suggestions = []; //alert(JSON.stringify(data.ResultSet.Result));
$.each(data.ResultSet.Result, function(i, val) {
suggestions.push(val.symbol+ " "+ val.name);
});
response(suggestions);
}
},
minLength: 1,
select: function (event, ui) {
//alert(ui.item.value.split("#")[1]);
$(this).val(ui.item.value.split(" ")[0]);
$("#stockvalue").val(ui.item.name);
return false;
},
});</script>
</body>
</html>
Javascript uses for autosuggest ticker names. Javascript用于自动建议的报价器名称。 After user input some letter in
input field
it sends ajax
request to yahoo finance
and should show the result. 用户在
input field
输入一些字母后,它将ajax
请求发送到yahoo finance
并应显示结果。
If i open this html
directly in browser - it works fine and shows some suggestions. 如果我直接在浏览器中打开此
html
,效果很好,并显示了一些建议。 But after i use it in my django
project it shows nothing. 但是在我的
django
项目中使用它后,它什么也没显示。
My views.py
: 我的
views.py
:
def home(request):
return render(request, 'Test.html')
What i found out in firefox console: 1) If i open html
directly: after placing a letter in input field JS sends request to YAHOO.Finance
(the full link for letter a
: http://d.yimg.com/autoc.finance.yahoo.com/autoc?callback=YAHOO.Finance.SymbolSuggest.ssCallback&query=a®ion=US&lang=en-US&_=1467262652424
) with method GET
and it's working good, we have an answer in json. 我在firefox控制台中发现的内容:1)如果我直接打开
html
:在输入字段中放置一个字母后,JS将请求发送到YAHOO.Finance
(字母a
的完整链接: http://d.yimg.com/autoc.finance.yahoo.com/autoc?callback=YAHOO.Finance.SymbolSuggest.ssCallback&query=a®ion=US&lang=en-US&_=1467262652424
: YAHOO.Finance
http://d.yimg.com/autoc.finance.yahoo.com/autoc?callback=YAHOO.Finance.SymbolSuggest.ssCallback&query=a®ion=US&lang=en-US&_=1467262652424
)与方法GET
一起使用,效果很好,我们在json中有一个答案。 2) If we use Django
to render this template: It sends request to YAHOO too (the full link for letter a
: http://d.yimg.com/autoc.finance.yahoo.com/autoc?callback=YAHOO.Finance.SymbolSuggest.ssCallback&query=a®ion=US&lang=en-US&_=1467263364507
) but the status is 400 Bad Request
. 2)如果我们使用
Django
渲染此模板:它也会向YAHOO发送请求(字母a
的完整链接: http://d.yimg.com/autoc.finance.yahoo.com/autoc?callback=YAHOO.Finance.SymbolSuggest.ssCallback&query=a®ion=US&lang=en-US&_=1467263364507
: http://d.yimg.com/autoc.finance.yahoo.com/autoc?callback=YAHOO.Finance.SymbolSuggest.ssCallback&query=a®ion=US&lang=en-US&_=1467263364507
),但状态为400 Bad Request
。 Inside the answer from yahoo i see this: /**/YAHOO.Finance.SymbolSuggest.ssCallback({"error":{"result":null,"error":{"code":"request-error","description":"Unauthorized JSONP request"}}});
在yahoo的答案中,我看到以下内容:/
/**/YAHOO.Finance.SymbolSuggest.ssCallback({"error":{"result":null,"error":{"code":"request-error","description":"Unauthorized JSONP request"}}});
: /**/YAHOO.Finance.SymbolSuggest.ssCallback({"error":{"result":null,"error":{"code":"request-error","description":"Unauthorized JSONP request"}}});
: /**/YAHOO.Finance.SymbolSuggest.ssCallback({"error":{"result":null,"error":{"code":"request-error","description":"Unauthorized JSONP request"}}});
: /**/YAHOO.Finance.SymbolSuggest.ssCallback({"error":{"result":null,"error":{"code":"request-error","description":"Unauthorized JSONP request"}}});
:" /**/YAHOO.Finance.SymbolSuggest.ssCallback({"error":{"result":null,"error":{"code":"request-error","description":"Unauthorized JSONP request"}}});
What could be wrong?
有什么事吗
I found answer for my question: 我找到了我的问题的答案:
all you need is to add this in head: <meta charset="UTF-8" name="referrer" content="no-referrer">
您所需要做的就是将其添加到头部:
<meta charset="UTF-8" name="referrer" content="no-referrer">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.