【发布时间】:2012-10-06 06:20:03
【问题描述】:
我正在使用 AJAX 调用从 URL 获取数据。它给了我一个 json 对象。
当我运行该应用程序时,该页面在 IE 中运行良好,并且符合以下条件: 该页面正在访问不受其控制的信息。
这会带来安全风险。要继续吗?
但这不适用于其他浏览器,如 Firefox、Chrome、Safari 等。
我不知道是什么问题。请向我解释为什么会发生这种情况以及如何解决这个问题?
我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Search Engine</title>
<script src="JS/jquery-1.4.2.min.js"></script>
<script>
$(document).ready(function () {
$.support.cors = true;
// create a script tag element
var script = document.createElement("script");
// set the attribute, using the URL to pass data via query parameters
script.setAttribute("src", "http://192.168.13.111:7090/?uname=bhagirathip&wt=json&fl=*,score");
script.setAttribute("type", "text/javascript");
// add the script tag to the document head, forcing an HTTP request
document.getElementsByTagName("head")[0].appendChild(script);
});
function Search() {
function callbackJsonHandler(data) {
alert(data); // This is the JSON data
}
}
</script>
</head>
<body>
<form id="form">
<div style="text-align: center">
<input type="search" id="searchInput" autofocus />
<input type="button" id="btnSearch" onclick="Search()" value="Search" />
</div>
</form>
</body>
</html>
【问题讨论】:
-
正如
jmort253在下面的答案中指出的那样,您可以使用jsonp和callback来实现它,这应该像使用jquery一样容易。 -
我不清楚您要做什么。您仍在尝试进行跨域 ajax 调用,但这是行不通的,并且您还在 document.ready 中发出脚本标记请求,这将用于发出请求,但要检索数据,数据必须作为函数调用发送回浏览器。你需要一个
function callbackJsonHandler(data),就像我在回答中所说的那样,你的服务器必须发回一个callbackJsonHandler({ /*JSON goes here */);。它还有助于查看发送响应所涉及的服务器端代码。考虑将其添加到edit。 -
你能分享一步一步的方法吗?实际上我在 jquery 中很弱。我必须在服务器中更改??实际上我无权访问服务器
-
我添加了另一个描述代理方法的答案。它有点宽泛,并且您缺少诸如您正在使用的服务器端语言、谁拥有服务器以及其他重要细节等细节,但它应该可以帮助您入门。祝你好运!
-
我使用 asp.net C# 作为服务器端。
标签: ajax jquery cross-domain