HTML code:
<form method="post" action="process.php">
<button id="process" type="submit">Button</button>
</form>
JavaScript代码:
<script type="text/javascript">
$(function(){
$('#process').click(function(e){
e.preventDefault();
$form = $(this).closest('form');
$.ajax({
url: $form.attr('action'),
type: $form.attr('method'),
dataType: 'json',
success: function(responseJson) {
$form.before("<p>"+responseJson.parameter3+"</p>");
},
error: function() {
$form.before("<p>There was an error processing your request.</p>");
}
});
});
});
</script>
我的process.php页面打印以下代码:
{
"status" : "success",
"data" : {
"parameter1" : "hello",
"parameter2" : 42,
"parameter3" : "hello world",
"parameter4" : "hello again"
}
}
问题在于我得到了未定义的&#34;在调用AJAX时。为什么呢?
答案 0 :(得分:0)
试试这个;
$form.before("<p>"+responseJson.data.parameter3+"</p>");
您忘记data
访问parameter3
值