如何通过usng ajax请求获取html源代码 [英] how to get html source code by usng ajax request

查看:102
本文介绍了如何通过usng ajax请求获取html源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用ajax调用来获取粒子网址的html源代码,直到我完成了,



pre> url:http://google.com,
类型:GET,
dataType:jsonp,
context:document .doctype
})。done(function(data){
alert(data);
});

但在此代码中给出错误,

  SyntaxError:语法错误

<!doctype html>< html itemscope =itemtype =http://schema.or

我想通过此次通话阅读html,



如何我可以存档这个,或者其他任何方式来做这件事吗?



预先致谢

解决方案

你不能使用ajax从web的狂野世界中的任何地方获取一些随机内容......这是为ajax请求实现同源策略的原因之一......



正常情况下,如果远程资源想要在浏览器环境中跨域访问,那么它们将实现jsonp或CORS ...但这两种技术都必须由目标资源来实现。 ..



在您的情况下,从错误中可以清楚地看到远程资源没有实现jsonp ......



<一个可能的解决方案是让您的Web服务器充当代理...即您向服务器发送请求,该请求将向远程资源发送请求并获取远程内容然后将其发送回客户端


I want to get html source of particulate url by using ajax call,

till I have done,

   url: "http://google.com",
            type: "GET",
            dataType: "jsonp",
            context: document.doctype
        }).done(function (data) {
            alert(data);
        });

but in this code its give error,

SyntaxError: syntax error

<!doctype html><html itemscope="" itemtype="http://schema.or

I want to read html from this call,

How can I achive this , or any other way to do like this?

Thanks in advance

解决方案

You can't use ajax to fetch some random content from anywhere in wild world of web... that is one of the reason for implementing Same Origin Policy for ajax requests...

Normal circumstances if a remote resource wants to make itself available across domain in browser environment then they will implement either jsonp or CORS... but both these techniques has to be implemented by the target resource...

In your case it is clear from the error that the remote resource is not implementing jsonp...

One possible solution is to make your web server act like a proxy... ie you sent a request to your server which will sent a request to the remote resource and gets back the remote content then sent it back to the client

这篇关于如何通过usng ajax请求获取html源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆