加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax(或者jquery)提交整个form表单

发布时间:2020-12-16 00:46:37 所属栏目:百科 来源:网络整理
导读:方法一: $.ajax({ cache: true,type: "POST",beforeSend: function(request) { request.setRequestHeader("userId","123456");},url:"localhost:8080/PBR_SERVICE/rest/upload/imgUpload?type=1",data:$('#yourformid').serialize(),// 你的formid async: f
方法一:
$.ajax({
        cache: true,type: "POST",beforeSend: function(request) {
		request.setRequestHeader("userId","123456");
	},url:"localhost:8080/PBR_SERVICE/rest/upload/imgUpload?type=1",data:$('#yourformid').serialize(),// 你的formid
        async: false,error: function(request) {
               alert("Connection error");
        },success: function(data) {
		//其中data的结构:{"error":"1,"success":null},可以将后台的处理结果放在这个JSON对象的2属性中,在前台回去回应给用户
               $("#appcreshi").parent().html(data.success);
        }
});
好处:

1、serialize()方法通过序列化表单值,创建URL编码文本字符串,不需要用JSON对象形式{"userId":,"123",“mid”:"23"......}将每个参数都作为属性传过去,这对多表单域的form提交很有好处;同时,对于图片的上传,不用考虑如何传图片二进制值的问题。

2、因为是ajax请求,可以加请求头

3、可以将后台处理的结果回应给用户

方法二:
 $.post(
	url,
	{"":,"random":Math.random()},
	function(data){
		alert(data.success);
	}
);
弊端:需要用JSON对象形式{"userId":,“mid”:"23"......}将每个参数都作为属性传过去,这对多表单域的form提交很不方便,且不能加请求头

方法三:

var options ={   
                    url:'localhost:8080/PBR_SERVICE/rest/upload/imgUpload?type=1',
                    type:'post',data:null,success:function(data){
                   	 if(data.statusCode=="OK") {  
                   	
		   	 }else{

                   	 } 
                    }   
                 };
              var form =$("form[name=form1]");//form1:表单ID  在表单界面只用这一个表单ID
              form.ajaxSubmit(options);   
弊端:不能加请求头

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读