gpt4 book ai didi

javascript - 使用 AJAX 发布到同一页面时无法收到正确的响应

转载 作者:行者123 更新时间:2023-11-30 20:00:22 24 4
gpt4 key购买 nike

我正在尝试发布到同一页面并使用 AJAX 检索结果:

Ajax 调用:

$(document).ready(function() {
$("#createaccount").click(function(){
console.log("Clicked");
console.log(document.getElementById("textbox").value);
$.ajax({
type: "POST",
data: {'textAreaInput': document.getElementById("textbox").value},
success: function (data) {
if(data.status == "1") {
alert("we having a working script");
} else {
alert("Oops, script is a no go");
}

}
});
});
});

表格:

<form method="post" action='' class='form-horizontal' enctype='multipart/form-data' id='startform'>
<textarea id="textbox"></textarea>
<button class="btn btn-primary col-lg-12 col-xs-12 col-md-12" name="createaccount" id="createaccount" type="button">Create account</button>
</form>

PHP 处理程序:

<?php
if (isset($_POST['textAreaInput'])){
$result = array("status" => "1");
echo json_encode($result);
exit;
}
?>

我总是收到 Oops, script is a no go 作为警报,所以似乎出了点问题,但我不完全确定是哪一部分。我是处理错误还是以错误的方式发布?

最佳答案

这里是 test.php:

<?php
if (isset($_POST['textAreaInput'])){
$result = array("status" => "1");
echo json_encode($result);
exit;
}
?>
<form method="post" action='' class='form-horizontal' enctype='multipart/form-data' id='startform'>
<textarea id="textbox"></textarea>
<button class="btn btn-primary col-lg-12 col-xs-12 col-md-12" name="createaccount" id="createaccount" type="button">Create account</button>
</form>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#createaccount").click(function(){
console.log("Clicked");
console.log(document.getElementById("textbox").value);
$.ajax({
type: "POST",
data: {'textAreaInput': document.getElementById("textbox").value},
dataType: 'json',
success: function (data) {

if(data.status == "1") {
alert("we having a working script");
} else {
alert("Oops, script is a no go");
}

}
});
});
});
</script>

关于javascript - 使用 AJAX 发布到同一页面时无法收到正确的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53446841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com