我正在开发一个 html 页面,该页面应该向我的服务器提交带有请求正文的 post 请求,如下所示
<html>
<head>Customer app</head>
<body>
<div>
<table>
<tr>
<td>Customer Id :</td>
<td>
<form name="submitform" method="post">
<input type="text" id="customerId" name="customerId"/>
<input type="submit" value="Submit">
</form>
</td>
</tr>
</table>
</div>
</body>
<script>
$(document).ready(function(){
$("#submitform").click(function(e)
{
var MyForm = JSON.stringify($("#customerId").serializeJSON());
console.log(MyForm);
$.ajax({
url : "http://localhost:7777/ola-drive/customer/ride",
type: "POST",
data : MyForm,
});
e.preventDefault(); //STOP default action
});
});
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
它无法按预期工作,抛出 404 Not Found 重定向到http://localhost:7777/customerapp.html。但请求提交对应的表单数据似乎是正确的。
有人可以帮我解决 html 代码提交 POST 请求重定向的问题吗?
我有一个提交表单并加载警报等的Javascript文件.现在我想添加调用或获取.php文件的功能,我尝试使用$.get("mail.php");但它似乎不起作用.
Javascript代码:
$('#myform').on('submit', function(e){
e.preventDefault();
$.get($(this).attr('action') + $(this).serialize());
alert("Thank You! \nTop Up for: <?php echo "$username" ?> Purchased Successfully");
$.get("mail.php"); //this is the added part to get mail.php//
location.reload();
Run Code Online (Sandbox Code Playgroud)
PHP - mail.php:
<?php
$to = "stephan@mail.co.za";
$headers = "From: website mail \r\n";
$email_body = "has just toped up his account.\n".
$email_subject = "User Topup Alert";
mail($to,$email_subject,$email_body,$headers);
?>
Run Code Online (Sandbox Code Playgroud) 我在Mason组件(Am)中有一个HTML表单,它使用post方法调用另一个Mason组件(Bm).我希望这个Mason组件(Bm)将值返回到Mason组件(Am)中的HTML表单.然后我想将此返回值传递给Javascript函数.
我怎样才能做到这一点?我是网络开发的新手.
可能重复:
jQuery AJAX提交表单
我在页面A上有一个表单,而不是我发布到新页面,有没有办法,我可以做提交ajax请求它而不是?我想让页面能够将结果加载到div中.
我知道如何编写调用类型的脚本,但我不确定是否必须手写它或者如果可以获取所有表单信息并发送它而不必将其全部编码到属性中.我以为我可以做一些像:onClick,post form但将结果返回div.
思考?想法?
不知何故ajaxSubmit and ajaxForm有点扮演同样的角色.如果是这样,那么它们之间是否有任何显着差异?如果是这样; 哪个使用,何时以及为什么?