2014-09-20 74 views
0

我有这样的AJAX调用AJAX jquery返回消息?

$(function() { 
    $("button#submit").click(function(){ 
       $.ajax({ 
        type: "POST", 
      url: "process.php", 
      data: $("form.contact").serialize(), 
       success: function(msg){ 
        $(".alert-success").toggle(); 
        $("#form-content").modal("hide");  
       }, 
      error: function(){ 
       $(".alert-error").toggle(); 
       } 
        }); 
    }); 
}); 

的问题是,在process.php我只有

echo "OK"; 

在控制台中我看到resposne但没有页上,还有什么能问题?

回答

2

你得到msg作为ajax调用的结果,但你不使用它的任何地方。你可以在成功内部使用它:例如:

$(function() { 
    $("button#submit").click(function(){ 
       $.ajax({ 
        type: "POST", 
      url: "process.php", 
      data: $("form.contact").serialize(), 
       success: function(msg){ 
        $(".alert-success").toggle(); 
        $("#form-content").modal("hide"); 
        //console.log(msg); to get the result in console for example 
       }, 
      error: function(){ 
       $(".alert-error").toggle(); 
       } 
        }); 
    }); 
}); 
+0

不错,如何从php返回msg并使用它? – 2014-09-20 15:23:13

+0

我在回答中展示了你。你可以随心所欲地处理你的ajax调用的结果。插入一个'div' contaier,例如'$(“#example”).html(msg)'等。 – 2014-09-20 15:28:25

+0

是的,很好。如何从PHP返回mgs? – 2014-09-20 18:06:29