Javascript 获取HTML表单Post方法的返回值
我在一个Mason组件(a.m)中有一个HTML表单,它使用post方法调用另一个Mason组件(B.m)。我希望这个Mason组件(B.m)向Mason组件(a.m)中的HTML表单返回一个值。然后我想把这个返回值传递给Javascript函数Javascript 获取HTML表单Post方法的返回值,javascript,html,perl,mason,Javascript,Html,Perl,Mason,我在一个Mason组件(a.m)中有一个HTML表单,它使用post方法调用另一个Mason组件(B.m)。我希望这个Mason组件(B.m)向Mason组件(a.m)中的HTML表单返回一个值。然后我想把这个返回值传递给Javascript函数 我该怎么做?我是web开发新手。您需要提出AJAX请求。虽然不是严格必要的,但我建议您使用,这将使事情变得容易得多。请看一下这个问题: 这是Mason中的一个小例子,它非常简单,当然,您应该添加一些错误检查和一些转义,但我认为这可能是一个好的开始。您的
我该怎么做?我是web开发新手。您需要提出AJAX请求。虽然不是严格必要的,但我建议您使用,这将使事情变得容易得多。请看一下这个问题: 这是Mason中的一个小例子,它非常简单,当然,您应该添加一些错误检查和一些转义,但我认为这可能是一个好的开始。您的
A.mc
组件可能如下所示:
<html>
<head>
<title>This is A</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#myform").submit(function() { // intercepts the submit event
$.ajax({ // make an AJAX request
type: "POST",
url: "B", // it's the URL of your component B
data: $("#myform").serialize(), // serializes the form's elements
success: function(data)
{
// show the data you got from B in result div
$("#result").html(data);
}
});
e.preventDefault(); // avoid to execute the actual submit of the form
});
});
</script>
</head>
<body>
<form id="myform">
<input type="text" name="mytext" />
<input type="submit" />
</form>
<div id="result"></div>
</body>
</html>
结果如下:
<html>
<head>
<title>This is A</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#myform").submit(function() { // intercepts the submit event
$.ajax({ // make an AJAX request
type: "POST",
url: "B", // it's the URL of your component B
data: $("#myform").serialize(), // serializes the form's elements
success: function(data)
{
// show the data you got from B in result div
$("#result").html(data);
}
});
e.preventDefault(); // avoid to execute the actual submit of the form
});
});
</script>
</head>
<body>
<form id="myform">
<input type="text" name="mytext" />
<input type="submit" />
</form>
<div id="result"></div>
</body>
</html>
了解Ajax请求。