[英]get content from external url using javascript
I am using freetexthost.com to store my json code.. and now i ve to get those content from url using javascript,jquery,ajax... bt am being unable to get it.. am trying following code 我正在使用freetexthost.com来存储我的json代码..现在我不得不使用javascript,jquery,ajax从url获取那些内容。bt无法获取它。
<!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>
getting an error as `Uncaught SyntaxError: Unexpected token < 得到一个错误信息,如`Uncaught SyntaxError:Unexpected token <
is there any chances we can manipulate content of other page(url) using js... 有没有机会我们可以使用js来操纵其他页面的内容...
in your json file create function: 在您的json文件创建函数中:
//----for example parseResponse({"Name": "Foo", "Id": 1234, "Rank": 7});
then call this function by JSONP
然后通过JSONP调用此函数
var result = $.getScript("http://freetexthost.com/r56ct5aw03?callback=parseResponse");
I hope to help you. 希望对您有所帮助。
You need to close your url using "
: 您需要使用
"
关闭网址:
$.ajax({
type: "GET",
url: "https://http://freetexthost.com/r56ct5aw03", // <-- Here
dataType: "jsonp",
success: function(data){
console.log(data);
}
});
The content of the page http://freetexthost.com/r56ct5aw03 is html, it should be jsonp to parse properly 页面http://freetexthost.com/r56ct5aw03的内容是html,它应该是jsonp才能正确解析
The only difference between json and jsonp is that when calling jsonp you will also pass a callback parameter json和jsonp之间的唯一区别是,在调用jsonp时,您还将传递一个回调参数
e.g. url:"http://freetexthost.com/r56ct5aw03?callback=myFunction",
Now the server side should print the json enclosed in this function name like below. 现在,服务器端应打印此函数名称中包含的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"
}
]
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.