在不离开当前域的情况下将数据POST到不同的域形式


POST data to a different domain form without leaving current domain

我目前正在处理一个项目,该项目要求我将数据POST到属于不同域的表单中。这确实有效,但我不希望用户看到发布到不同域的数据,因为它只是显示一条成功/错误消息(在B页)。

我想做的是让数据POST,但页面不会更改。我读到AJAX可以用来做这件事,但前提是页面属于同一个域。windows.location.replaced()看起来很有希望,但这只有在我可以访问外部表单时才有效。

当前实施情况:用户在页面A上输入数据>POST数据到页面B,页面B加载

想要我想要实现:用户在页面A上输入数据>POST数据到页面B,页面A保持显示

使用cURL来执行此操作。

Page1.php

$url='http://www.domain.com/page2.php';
$params='username=jim&pass=jim321';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $result = curl_exec($ch); // This outputs the response in your same page i.e. Page1.php
curl_close($ch);

尝试使用类似的curl

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.otherdoamin.com/page.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"post1=val1&post2=val2&post3=val3");
// get server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);