使用javascript从外部url获取内容

使用javascript从外部url获取内容,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在使用freetexthost.com存储我的json代码。。 现在我必须使用javascript、jquery、ajax从url获取这些内容。。。 我无法得到它。。 我正在尝试下面的代码 <!DOCTYPE html> <html> <head> <title>Useless</title> <script type='text/javascript' src='http://code.jquery.com/jquery-1

我正在使用freetexthost.com存储我的json代码。。 现在我必须使用javascript、jquery、ajax从url获取这些内容。。。 我无法得到它。。 我正在尝试下面的代码

<!DOCTYPE html>
<html>
<head>
<title>Useless</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.11.0.min.js'></script>
<script type="text/javascript">

$.ajax({
  type:     "GET",
  url:      "http://freetexthost.com/r56ct5aw03",
  dataType: "jsonp",
  success: function(data){
    console.log(data);
  }
});

</script>
</head>
<body>

<div class="content" >Hello</div>

</body>
</html>

无用的
$.ajax({
键入:“获取”,
url:“http://freetexthost.com/r56ct5aw03",
数据类型:“jsonp”,
成功:功能(数据){
控制台日志(数据);
}
});
你好

将错误获取为“未捕获的语法错误:意外标记”


我们是否有可能使用js操纵其他页面(url)的内容…

您需要使用
关闭您的url:

$.ajax({
键入:“获取”,
url:“https://http://freetexthost.com/r56ct5aw03“,//页面内容是html,应该是jsonp才能正确解析

json和jsonp之间的唯一区别是,在调用jsonp时,还将传递回调参数

e.g. url:"http://freetexthost.com/r56ct5aw03?callback=myFunction", 
现在,服务器端应该打印这个函数名中包含的json,如下所示

myFunction(
    {
        "sites":
        [
            {
                "siteName": "123",
                "domainName": "http://www.123.com",
                "description": "123"
            },
            {
                "siteName": "asd",
                "domainName": "http://www.asd.com",
                "description": "asd"
            },
            {
                "siteName": "zxc",
                "domainName": "http://www.zxc.com",
                "description": "zxc"
            }
        ]
    }
);
  • 在json文件创建函数中:

     //----for example
     parseResponse({"Name": "Foo", "Id": 1234, "Rank": 7});
    
  • 然后通过JSONP调用此函数

  • 我希望能帮助你


    参考资料:

    您错过了
    url:"https://http://freetexthost.com/r56ct5aw03,
    假设它是typoUncaught SyntaxError:Unexpected token<此错误通常在json解析器尝试使用浏览器网络检查器解析HTMLUS时,不会返回jsonp。如果在浏览器中使用真实url,您会在html页面中看到原始json或json吗?那么我如何在中上载json文件免费上网,这样我以后就可以轻松地使用它??我不确定可能的重复,因为他在这里遇到了一个语法错误。我怎样才能在网上以jsonp的形式免费上传我的json文件,然后再轻松地解析呢?你需要一个像php这样的服务器端语言来打印jsonp函数名中包含的json文本。你只需要一个正常的宿主账户
     //----for example
     parseResponse({"Name": "Foo", "Id": 1234, "Rank": 7});
    
    var result = $.getScript("http://freetexthost.com/r56ct5aw03?callback=parseResponse");