【发布时间】:2018-05-16 16:56:02
【问题描述】:
我有一个由单击按钮触发的 ajax 调用,但即使一切看起来都很好,它有时也会停止工作。错误是 500 Internal Server,它非常广泛,所以我试图在我的函数中查找错误,但到目前为止没有运气。代码有什么问题吗?
这是我的 AJAX 调用:
jQuery(document).ready( function($){
$('.modal-footer').on('click', '#tempsave_submit', function() {
//Loads all the variables to be sent
var source = $("#source-save").text();
var name = $("input[id='pname-save']").val();
var stage = $("input[id='pstage-save']").val();
//Checks to see if any of those variables are empty
if (source == null || name == null || stage == null)
{$("#failstat").text("Error Reference Number: 300");
$("#failsave").slideDown(100).delay(2000).slideUp(100);
}
//If they are all valid, it calls the ajax function
else{
$.ajax({
url: 'https://website.com/tempsave',
type: 'POST',
data: {
source_save: source,
pname_save: name,
pstage_save: stage
},
dataType : 'html',
timeout: 30000,
cache: false,
success: function () {
$("#successsave").slideDown(100).delay(2000).slideUp(100);
},
error: function (jqXHR, exception) {
$("#failstat").text("Error Reference Number: 343-" + jqXHR.status + exception);
$("#failsave").slideDown(100).delay(2000).slideUp(100);
}
});
}
});
});
这是目标 php:
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$source = $_POST["source_save"];
$name = temp_input_check($_POST["pname_save"]);
$description = temp_input_check($_POST["pstage_save"]);
// tempsave_write() is a predefined function-no issues with this part
$fid = tempsave_write($source, $name);
// tempsave_save() is a predefined function-no issues with this part
$query = tempsave_save($name , $description , $fid);
}else{
$html = '
<h2>No Projects Were Found!</h2>
</div>
<p>Start by creating a new project. Your projects will be displayed here
once you submit them.</p>';
return $html;
}
当它给我 500 内部错误时,我检查了响应,它是从“else”返回的 html。这意味着服务器一开始并没有将其作为 POST 方法发送。那可能吗。 @Taplan 帮助了我,我们发现方法是 GET!这怎么可能?
更新: 我不得不更改我的 html 代码的某些部分(没有触及与 ajax 调用相关的代码),现在 ajax 仅通过 GET 方法发送它,因此会出现 500 错误。正在发送的参数之一是已更改的 html 源代码,现在它作为 GET 发送。我试图缩小范围,发现参数甚至不能作为 GET 使用,即使页面上说它们是作为 GET 发送的。现在我确信问题是由于发送了 html 源代码,但不知道它有什么问题。有什么帮助吗?
【问题讨论】:
-
登录
$_SERVER["REQUEST_METHOD"]看看它是什么。我很好奇您是否收到 OPTIONS 请求的错误,该请求有时会出现在 POST 和 PUT 之前 -
该 PHP 代码是否在函数中?如果不是,则将
return更改为echo $html返回不会将任何内容发送回浏览器 -
是的,它在一个函数中。我只是复制了里面的代码。
-
@Taplar,我记录了它。发送的方法是 GET。现在为什么会发生这种情况?
-
更新了原来的问题
标签: php jquery ajax post drupal-7