你的AJAX调用将通过邮局发送数据或GET,那么你可以做的是,也什么返回一些东西给你的脚本。这很简单。
http://api.jquery.com/jquery.ajax/
让我们以实例的工作。如果你想使A + B的服务器上,你需要有一个表格,像这样输入:
<form id="yourform">
<div><input name="A" type="text" /></div>
<div><input name="B" type="text" /></div>
</form>
然后你会编程,一些脚本说你的表单会做什么提交。当你使用jQuery时,让我们使用jQuery:
$("#yourform").submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST", //or "GET", if you want that
url: "yourfile.php",
data: $(this).serializeArray(), //here goes the data you want to send to your server. In this case, you're sending your A and B inputs.
dataType: "json", //here goes the return's expected format. You should read more about that on the jQuery documentation
success: function(response) { //function called if your request succeeds
//do whatever you want with your response json;
//as you're learning, try that to see on console:
console.log(response);
},
error: function(response) { //function called if your request failed
//do whatever you want with your error :/
}
});
});
但是你在服务器上做什么? 那么,我只是想返回我的输入,只是为了检查。这里是我的PHP:
<?php
header("Content-type: application/json; charset=utf-8"); //that's required to return any JSON data
if(isset($_POST, $_POST['A'], $_POST['B']))
exit(json_encode($_POST));
else
exit("INVALID REQUEST.");
?>
这就是您可以通过AJAX发送信息以在PHP上执行某些操作的方式。
看起来你实际上并没有将任何数据传递给'create_product.php'。你需要''.ajax'选项中的'data'键,其中包含你想要在你的PHP脚本中使用的信息,这些信息可以通过$ _POST' –
得到,你能否给我一个清楚的答案?我如何通过'data'利用'$ _POST'以及它应该包含哪些内容? –
向Ajax调用添加一个错误处理程序。你也应该取消butotn点击 – epascarello