简体   繁体   English

用于发布或获取URL并返回页面上文本的Javascript

[英]Javascript to make Post or Get call to a URL and return the text that's on the page

I've been trying to do this all morning I need to make either a POST or a GET call to this 我一直在尝试这样做,我需要对此进行POST或GET调用

URL http://cabbagetexter.com/send.php 网址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 我需要它来返回页面上的文本,我知道这不是那么困难,但是我完全被这一代码阻止了,我已经尝试过使用JQuerys .post和.get函数,但是我似乎无法返回只是页面上的文字

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 球,谢谢大家的帮助

(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 显然,由于相同的原始策略,您需要在要加载的URL上托管此脚本

You are running into the cross domain limitation. 您正在遇到跨域限制。 You can only to a request to a page in the same domain. 您只能向同一域中的页面请求。

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 了解如何使用AJAX

If not, then, sorry, you're out of luck because you'll run into the same origin policy error. 如果没有,那么对不起,您不走运,因为您会遇到相同的原始策略错误。

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. 假设您的Javascript是从index.php运行的。 You might create a file called ctexter.php. 您可能会创建一个名为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. ctexter.php将使用curl来向http://cabbagetexter.com/send.php发出发布请求,然后输出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. 因此,如果index.php对ctexter.php进行ajax调用,而ctexter.php从send.php输出响应,则实际上已经实现了您的目标。

You could make the curl post requests with this function: 您可以使用以下功能发出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;
}

where $url is (obviously) the url to post to, and $data is an associative array containing the data you want to submit. 其中$ url(显然)是要发布到的URL,而$ data是包含要提交的数据的关联数组。

Then on ctexter.php you could do something like: 然后在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";
}

Finally, hit ctexter.php using JQuery .post() : 最后,使用JQuery .post()打ctexter.php:

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

There is a way to make a request to that URL and get around the same origin policy. 有一种向该URL发出请求并绕过相同原始策略的方法。 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. 将类似PHP脚本的内容放在您自己的域中,该域向http://cabbagetexter.com/send.php发出请求,然后从javascript调用您自己的脚本。

If your host supports PHP and cURL a script like this would do the job: 如果您的主机支持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;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM