Javascript 对 URL 进行 Post 或 Get 调用并返回页面上的文本
我整个早上都在尝试这样做,我需要对此
URL http:// /cabbagetexter.com/send.php
我需要它返回页面上的文本,我知道这不会那么困难,但我完全被代码阻止了, 我尝试过使用 JQuerys .post 和 .get 函数,但我似乎无法仅返回页面上的文本,
任何帮助都会被应用
编辑: 啊,好吧,因为技术原因无法做到这一点。球,谢谢大家的帮助
I've been trying to do this all morning I need to make either a POST or a GET call to this
URL http://cabbagetexter.com/send.php
I need it to return the text that's on the page, I know it can't be that difficult but I'm completely code blocked on this one,
I've tried using JQuerys .post and .get functions but I cant seem to return just the text on the page
any help would be appriciated
EDIT:
Ah ok so there's a technical reason for not being able to do it. balls, Thanks for the help guys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
显然,由于相同的 源策略,您需要在正在加载的 URL 上托管此脚本
Obviously you need to host this script on the URL you are loading because of the same origin policy
您遇到了跨域限制。您只能向同一域中的页面发出请求。
You are running into the cross domain limitation. You can only to a request to a page in the same domain.
如果您需要向另一个域上的页面发出调用,还有另一种可能性。假设您的 Javascript 是从 index.php 运行的。您可以创建一个名为 ctexter.php 的文件。
ctexter.php 将使用curl 向 http://cabbagetexter.com/send.php 发出 post 请求,然后输出响应(来自 send.php 的输出)。因此,如果index.php 对ctexter.php 进行ajax 调用,并且ctexter.php 正在输出send.php 的响应,那么您实际上已经实现了目标。
您可以使用此函数发出curl post请求:
其中$url(显然)是要发布到的url,$data是包含您要提交的数据的关联数组。
然后在 ctexter.php 上你可以这样做:
最后,使用 JQuery .post()< 来点击 ctexter.php /a>:
There is another possibility if you need to post calls to a page on another domain. Let's say your Javascript is being run from index.php. You might create a file called ctexter.php.
ctexter.php would use curl to make the post request to http://cabbagetexter.com/send.php, and would then output the response (the output from) send.php. So, if index.php makes an ajax call to ctexter.php, and ctexter.php is outputting the response from send.php, you have in effect achieved your goal.
You could make the curl post requests with this function:
where $url is (obviously) the url to post to, and $data is an associative array containing the data you want to submit.
Then on ctexter.php you could do something like:
Finally, hit ctexter.php using JQuery .post():
如果您位于同一域,则可以使用如下代码:
了解如何使用 AJAX
如果没有,那么,抱歉,你运气不好,因为你会遇到 同源策略错误。
If you're on the same domain, you'd use some code like this:
Learn how to use AJAX
If not, then, sorry, you're out of luck because you'll run into the same origin policy error.
有一种方法可以向该 URL 发出请求并绕过同源策略。在您自己的域上放置类似 PHP 脚本的内容,该脚本向 http://cabbagetexter.com/send.php< /a> 然后从 javascript 调用您自己的脚本。
如果您的主机支持 PHP 和 cURL,这样的脚本就可以完成这项工作:
There is a way to make a request to that URL and get around the same origin policy. Put something like a PHP script on your own domain that makes a request to http://cabbagetexter.com/send.php and then call your own script from the javascript.
If your host supports PHP and cURL a script like this would do the job: