【发布时间】:2017-08-09 20:14:30
【问题描述】:
我有一个页面 index.php -> php 代码、jquery 代码和 php 代码合二为一。
$.ajax({
url: url,
method: "post",
contentType: "application/json",
dataType: "json",
data: JSON.stringify(inputData),
success: function(data, status, xhr) {
if (data.result) {
// I want to store this data.result and pass it to PHP code below.
$("#some-form").submit();
} else {
// some code here
}
},
error: function(data, status, xhr) {
// Show a modal informing user of invalid
information
}
})
我在 data.result 的成功块中看到正确的数据。 我想将此值传递给下面的 php 代码:
<?php
function buildRedirectUrl($url) {
return $url .
"&email=" . trim($_POST["email"]) .
"&state=" . trim($_POST["state"]) .
"&application_code=" .
trim($_POST["application_code"]);
// I want to add the data.result from success block here
// something like :
// "&result=" . trim($_POST["data.result"]) .
}
$redirectUrl = buildRedirectUrl($someUrl);
header('Location: ' . $redirectUrl);
?>
有什么解决办法吗? 是否可以将 ajax 调用中的数据传递给 php? 任何示例都会有所帮助。我找到了所有访问 inputParameters 的示例,但我没有看到任何关于如何在 php 中使用响应的示例
【问题讨论】:
-
我猜如果你删除
JSON.stringify如果工作正常?再说一次,我不知道你想做什么? -
我能够成功发出 POST 请求。我在成功块中看到了正确的结果。我想存储响应 data.result 并在我的 PHP 代码中访问它。
-
您为什么要这样做?在提交之前,您需要将结果存储在
#some-form中。为什么你首先要在 ajax 中做任何事情。只需使用 inputData 提交表单。 -
我想使用 header() 在另一个 html 页面中自动填充该字段...
-
使用具有这种模式的现有代码库:(