我整个上午都在尝试这样做,我需要对此进行POST或GET调用
URL http://cabbagetexter.com/send.php
我需要它来返回页面上的文本,我知道这不会那么困难,但我在这一点上完全被代码阻塞了,我尝试过使用JQuerys .post和.get函数,但似乎不能只返回页面上的文本
任何帮助都会得到重视
编辑:啊,好吧,所以不能这样做是有技术原因的。宝贝儿,谢谢你们的帮助
发布于 2011-12-23 01:37:34
如果您需要发布对另一个域上的页面的调用,还有另一种可能性。假设您的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
$.post("ctexter.php",
{
firstparamname: "firstparamvalue",
somethingelse: "llama"
},
function(data) {
alert("It worked! Data Loaded: " + data);
});发布于 2011-12-23 01:02:47
(function ($) {
$.ajax({
url: 'http://cabbagetexter.com/send.php',
type: 'text',
success: function (response) {
//do something with the text from the site
}
});
}(jQuery));显然,由于相同的origin policy,您需要将此脚本托管在要加载的URL上。
发布于 2011-12-23 01:03:22
您遇到了跨域限制。您只能将请求发送到同一域中的页面。
https://stackoverflow.com/questions/8607284
复制相似问题