我一直在尝试所有的早晨,我需要做POST或一个GET调用这个JavaScript来让POST或GET调用一个网址,并返回这页
URL http://cabbagetexter.com/send.php做到这一点的文字
我需要它返回页面上的文本,我知道它不是那么困难,但我完全在这个代码上被阻止, 我试过使用JQuerys .post和.get函数,但是我似乎不能返回只是在页面上的文字
任何帮助将appriciated
编辑: 阿好吧,所以有一个技术原因不能做到这一点。球,感谢您的帮助球员
我一直在尝试所有的早晨,我需要做POST或一个GET调用这个JavaScript来让POST或GET调用一个网址,并返回这页
URL http://cabbagetexter.com/send.php做到这一点的文字
我需要它返回页面上的文本,我知道它不是那么困难,但我完全在这个代码上被阻止, 我试过使用JQuerys .post和.get函数,但是我似乎不能返回只是在页面上的文字
任何帮助将appriciated
编辑: 阿好吧,所以有一个技术原因不能做到这一点。球,感谢您的帮助球员
如果您需要将呼叫发布到另一个域上的页面,还有另一种可能性。假设您的Javascript正在从index.php运行。您可能会创建一个名为ctexter.php的文件。 ctexter.php将使用curl将请求发送到http://cabbagetexter.com/send.php,然后输出send.php的响应(输出)。所以,如果index.php对ctexter.php进行ajax调用,并且ctexter.php正在输出来自send.php的响应,那么您实际上已经实现了您的目标。
你可以把这个功能卷曲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;
}
其中$网址是(显然)网址张贴到,和$数据是包含要提交数据的关联数组。
然后在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";
}
最后,打ctexter.php使用JQuery .post():
$.post("ctexter.php",
{
firstparamname: "firstparamvalue",
somethingelse: "llama"
},
function(data) {
alert("It worked! Data Loaded: " + data);
});
(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此脚本。您只能向同一个域中的页面发送请求。
如果你在同一个域,你会使用像这样的代码:
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();
如果没有,那么,对不起,你的运气了,因为你”会遇到same origin policy错误。
有一种方法,使该URL的请求和绕过相同的原产地政策。在您自己的域上放置一个类似PHP脚本的东西,请求http://cabbagetexter.com/send.php,然后从javascript调用您自己的脚本。
如果你的主机支持PHP和卷曲这样的脚本将做的工作:
<?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;
?>
我不知道,限制的,我有我需要用Java编写的,其作品完美的代码,但我现在需要它在JS中,我想回到绘图板 – Scott 2011-12-22 17:14:04