通过JQuery Ajax发布请求提交表单 [英] Submitting Form via JQuery Ajax Post Request

查看:159
本文介绍了通过JQuery Ajax发布请求提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Web Node/Express Web应用程序上具有基本的消息传递服务,并且我正在尝试使用FormData对象通过Ajax提交表单.

I have a basic messaging service on a web Node / Express web app, and I'm trying to submit the form via Ajax, using the FormData object.

如果我提交的表单不带AJAX,则一切正常,但带AJAX的req.body.都是不确定的.

If I submit the form without AJAX, then everything works fine, but with AJAX the req.body. are all undefined.

在服务器上,使用AJAX时,我需要在req.body以外的地方查找数据?

On the server, I need to look for the data somewhere other than req.body when using AJAX??

创建FormData对象:

Creating the FormData object:

var ajaxData = new FormData;
ajaxData.append('newMessage', $('.new-message').val()) // I've console.logged these, and their values are correct
ajaxData.append('senderId', $('[name="senderId"]').val())
ajaxData.append('senderName', $('[name="senderName"]').val())// I've console.logged these, and their values are correct
ajaxData.append('recipientId', $('[name="recipientId"]').val())
ajaxData.append('recipientName', $('[name="recipientName"]').val())// I've console.logged these, and their values are correct

这是POST请求:

$.ajax({
  url: $form.attr('action'),
  type: $form.attr('method'),
  data: ajaxData,
  dataType: false,
  cache: false,
  contentType: false,
  processData: false,
  complete: function() {
    console.log('message created');
  },
  success: function(data) {


  },
  error: function(xhr, textStatus, errorThrown) {
    console.log(xhr.statusText);
    console.log(textStatus);
    console.log(errorThrown);
  }
});

编辑

感谢G. Mansour在下面的回答.万一其他人到达这里,问题就在这行:

Thanks to G. Mansour for his answers below. In case anyone else gets here, the issue was the line:

contentType: false,

我在某些时候尝试过此行,这也行不通

I tried this line at some point, which also doesn't work

contentType: 'application/json',

但是当我完全删除该行时,一切都正常运行...如果有人可以告诉我为什么该行破坏了所有内容,那么我很想知道.

But when I remove the line entirely, everything is working as normal... If anyone can tell me why this line was breaking everything, I'd be interested to know.

推荐答案

这是html部分

<form id="form" action="" method="post">
<input type="text" name="msgID" id="msgID">
<input type="text" name="senderId" id="senderId">
<input type="text" name="senderName" id="senderName">
<input type="text" name="recipientId" id="recipientId">
<input type="text" name="recipientName" id="recipientName">
<input type="submit" name="dsq" value="dsqdsq">
</form>

这是JavaScript部分

this is the JavaScript part

<script type="text/javascript">

$(document).ready(function(){
$("#form").submit(function(){
    $.ajax({
        url: "test.php", 
        data: $("#form").serialize(), 
        type: "POST", 
        dataType: 'json',
        success: function (e) {
            console.log(JSON.stringify(e));


        },
        error:function(e){
            console.log(JSON.stringify(e));


        }
    }); 
    return false;
});
});

</script>

这是php代码

<?php 
die(json_encode(array("status"=>true)));
?>

希望对您有帮助.

这篇关于通过JQuery Ajax发布请求提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆