Javascript 对 URL 进行 Post 或 Get 调用并返回页面上的文本

发布于 2024-12-22 10:26:49 字数 288 浏览 0 评论 0原文

我整个早上都在尝试这样做,我需要对此

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如此安好 2024-12-29 10:26:49
(function ($) {
  $.ajax({
    url: 'http://cabbagetexter.com/send.php',
    type: 'text',
    success: function (response) {
      //do something with the text from the site
    }
  });
}(jQuery));

显然,由于相同的 源策略,您需要在正在加载的 URL 上托管此脚本

(function ($) {
  $.ajax({
    url: 'http://cabbagetexter.com/send.php',
    type: 'text',
    success: function (response) {
      //do something with the text from the site
    }
  });
}(jQuery));

Obviously you need to host this script on the URL you are loading because of the same origin policy

痴者 2024-12-29 10:26:49

您遇到了跨域限制。您只能向同一域中的页面发出请求。

You are running into the cross domain limitation. You can only to a request to a page in the same domain.

羁客 2024-12-29 10:26:49

如果您需要向另一个域上的页面发出调用,还有另一种可能性。假设您的 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请求:

function post_request($url, $data) {
    $output = array();
    foreach ($data as $key => $value) {
        if(is_object($value) || is_array($value)){
            $data[$key] = serialize($value);
        }
    }
    $output = array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    if ($result) {
        $output['status'] = "ok";
        $output['content'] = $result;
    } else {
        $output['status'] = "failure";
        $output['error'] = curl_error($ch);
    }
    curl_close($ch);
    return $output;
}

其中$url(显然)是要发布到的url,$data是包含您要提交的数据的关联数组。

然后在 ctexter.php 上你可以这样做:

// Since we already built the post array in the
// ajax call, we'll just pass it right through
$response = post_request("http://cabbagetexter.com/send.php", $_POST);
if($response['status'] == "ok"){
    echo $response['content'];
}
else{
    echo "It didn't work";
}

最后,使用 JQuery .post()< 来点击 ctexter.php /a>:

$.post("ctexter.php", 
{ 
    firstparamname: "firstparamvalue", 
    somethingelse: "llama" 
},
function(data) {
  alert("It worked! Data Loaded: " + data);
});

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:

function post_request($url, $data) {
    $output = array();
    foreach ($data as $key => $value) {
        if(is_object($value) || is_array($value)){
            $data[$key] = serialize($value);
        }
    }
    $output = array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    if ($result) {
        $output['status'] = "ok";
        $output['content'] = $result;
    } else {
        $output['status'] = "failure";
        $output['error'] = curl_error($ch);
    }
    curl_close($ch);
    return $output;
}

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:

// Since we already built the post array in the
// ajax call, we'll just pass it right through
$response = post_request("http://cabbagetexter.com/send.php", $_POST);
if($response['status'] == "ok"){
    echo $response['content'];
}
else{
    echo "It didn't work";
}

Finally, hit ctexter.php using JQuery .post():

$.post("ctexter.php", 
{ 
    firstparamname: "firstparamvalue", 
    somethingelse: "llama" 
},
function(data) {
  alert("It worked! Data Loaded: " + data);
});
抚你发端 2024-12-29 10:26:49

如果您位于同一域,则可以使用如下代码:

var ajax = new XMLHttpRequest();
ajax.onreadystatechange=function()
  {
  if (ajax.readyState==4 && ajax.status==200)
    {
    document.getElementById("targetElementID").textContent = ajax.responseText;
    }
  }
ajax.open("GET","http://cabbagetexter.com/send.php",true);
ajax.send();

了解如何使用 AJAX

如果没有,那么,抱歉,你运气不好,因为你会遇到 同源策略错误。

If you're on the same domain, you'd use some code like this:

var ajax = new XMLHttpRequest();
ajax.onreadystatechange=function()
  {
  if (ajax.readyState==4 && ajax.status==200)
    {
    document.getElementById("targetElementID").textContent = ajax.responseText;
    }
  }
ajax.open("GET","http://cabbagetexter.com/send.php",true);
ajax.send();

Learn how to use AJAX

If not, then, sorry, you're out of luck because you'll run into the same origin policy error.

柠檬色的秋千 2024-12-29 10:26:49

有一种方法可以向该 URL 发出请求并绕过同源策略。在您自己的域上放置类似 PHP 脚本的内容,该脚本向 http://cabbagetexter.com/send.php< /a> 然后从 javascript 调用您自己的脚本。

如果您的主机支持 PHP 和 cURL,这样的脚本就可以完成这项工作:

<?php
$url="http://cabbagetexter.com/send.php";
$post="";   
if(strstr($url,"?")){
    $split=explode("?",$url);
    $url=$split[0];
    $post=$split[1];
}
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0); 

if($post!=""){
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}else{
    curl_setopt($ch, CURLOPT_POST, 0); 
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
$result = curl_exec($ch);
curl_close($ch); 
print $result;
?>

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:

<?php
$url="http://cabbagetexter.com/send.php";
$post="";   
if(strstr($url,"?")){
    $split=explode("?",$url);
    $url=$split[0];
    $post=$split[1];
}
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0); 

if($post!=""){
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}else{
    curl_setopt($ch, CURLOPT_POST, 0); 
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
$result = curl_exec($ch);
curl_close($ch); 
print $result;
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文