简体   繁体   中英

jQuery Ajax = success return page

Ok so I know long hand ajax but trying to use the jQuery short cut. I have two documents

  1. form.php
  2. submit.php

In my "form" page I am calling the "submit" page to process the insert. I am currently using the jquery ajax:

   <script type="text/javascript"> 
   jQuery('form').submit(function() {   
   string = jQuery("form").serializeArray();
   jQuery.ajax({
   type: "POST",
   url: "submit.php", 
   data: string,
   dataType: "json",
   })   
   return false; 
   }); 
   </script>

When I view firebug it is processing the ajax fine. I am getting 200 and post parameters are set. What I am trying to do is have the ajax return the submit.php file. I know it has something to do with the "success" function but I don't know how to add this. I tried a few things like:

   <script type="text/javascript"> 
   jQuery('form').submit(function() {   
   string = jQuery("form").serializeArray();
   jQuery.ajax({
   type: "POST",
   url: "submit.php", 
   data: string,
   dataType: "json",
   success: function(html){
   alert(html);
   }
   })   
   return false; 
   }); 
   </script>

and

   <script type="text/javascript"> 
   jQuery('form').submit(function() {   
   string = jQuery("form").serializeArray();
   jQuery.ajax({
   type: "POST",
   url: "submit.php", 
   data: string,
   dataType: "json",
   success: function(html){
   $('.result').html(data);
   }
   })   
   return false; 
   }); 
   </script>

but neither of these are working. Again I am simply trying to send the ajax request and then return the contents of the submit.php page. Not only does the submit.php page hold the script to process the php/ajax insert but it also display success statements like "insert was successful" so that is why I need to not only run the script in the page but also return the contents of that page. Thank you for any help.

Chagne的dataType:'json'dataType:'html'callback ,你要显示的内容submit.php

You were close in your second attempt, but you made a typo. Try:

success: function (data) {
    $('.result').html(data);
}

Also, unless your server is returning JSON, you probably want to change the dataType :

dataType: "html"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM