jquery可以在我的localhost上运行,但不能在其他站点运行

时间:2012-08-26 07:19:37

标签: jquery get

嗨,我这里有问题 这是我的代码,它在我写“http:// localhost”并检索内容时工作,但是当我写其他网站如“http://www.google.com”时它不起作用。 我已经在我的网络主机中上传了代码供你们测试。 链接在这里。 http://faceproishere.netii.net/test.php 在这里你可以看到我的代码:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
</head>

<body>
<input type="text" name="field1" id="field1" value="http://localhost">

<input value="load" type="button" onClick="my_function()">


<textarea cols="15" rows="12" id="frame1" style="float:left"></textarea>
<div id="frame2" style="width:400px;height:400px;overflow:scroll;fl  oat:left"></div>


<script type="text/javascript">
function my_function()
{
    if($("#field1").val() != '' && $("#field1").val().indexOf("http",0) == 0)
    {

        $.get($("#field1").val(),function(data)
        {
            $("#frame1").html(data)
            $("#frame2").html(data)
        })

    }
}
</script>
</body>
</html>
提前Tnx。

2 个答案:

答案 0 :(得分:3)

您无法以其他方式从其他来源访问内容。出于安全原因,不允许这样做。另见:https://developer.mozilla.org/en-US/docs/Same_origin_policy_for_JavaScript

如果您检查浏览器的控制台,则可以非常轻松地看到此类问题。

答案 1 :(得分:0)

如果你有权访问curl,你可以设置一个curl调用来检索数据或地址。

curl是大多数Linux发行版上都提供的bash工具。它可以在http://curl.haxx.se/找到。

您所做的是通过ajax设置本地请求,该请求将结果传递给您使用curl获取的网站。在大多数脚本语言中,可以通过shell调用Curl。如果你已经安装了curl并需要脚本帮助,请告诉我langauges和我可能会帮助你。

好的她是一个解释。它可能不适合你,当我这样做它是在红宝石sinatra它极其容易设置路径和捕获链接的参数。在php中可能会有点困难。

首先,你需要一些方法来设置调用使用curl的php脚本的本地路径。最简单的方法是简单地将文件添加到Web路径(取决于Web服务器和框架)。

在curl_test.php文件中你可以做类似的事情:

$response = system('curl www.google.se', $returnvalue);
echo($response)

然后,您将设置ajax请求以运行该脚本:

$.ajax({
  url: "localhost/curl_test.php",
  data: data,
  success: success,
  dataType: dataType
});

接下来的技巧是如何将正确的数据放入数据部分,以便您可以将获取变量发送到curl脚本,但是如果你想要一个ruby sinatra示例,那就让你解决这个问题。

get '/data/:brand' do
  data_link =  "https://someplace.com/brands/#{params[:brand]}"
  c = Curl::Easy.perform(data_link)
  content_type :json
  c.body_str
end
相关问题