ajax的各种请求方式(get,post,put)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yufengaotian/article/details/82190708
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>List与数组直接绑定自定义数据类型与AJAX</title>
</head>
<body>

    <form action="http://localhost:9015/u9_app_api/imgs/imgupload" enctype="multipart/form-data" method="post">  
    <table>  
        <tr>  
        <td><input type="file" name="file"/></td> 
        </tr>  
        <tr>  
        <td><input type="file" name="file"/></td>  
        </tr> 
        <tr>  
        <td><input type="file" name="file"/></td> 
        </tr>  
        <tr>  
        <td><input type="file" name="file"/></td>  
        </tr>
        <tr>  
        <td>imgs_type:<input name="imgs_type"></td> 
        </tr>  
        <tr>  
        <td><input type="submit" value="上传所有图片"/></td>   
        </tr>  
    </table>  
	</form>
<br/>

<hr/>
8081 img 
<form action="http://192.168.10.16:8081/u9_app_api/imgs" enctype="multipart/form-data" method="post">
	file:<input type="text" value="22222" style=" color:red; background-color:#000;"><br/>	
	imgs_type:<input name="imgs_type"><br/>
	<input type="submit">
</form> <br/>


<hr/>
img 
<form action="http://localhost:9015/u9_app_api/imgs/upload_img" enctype="multipart/form-data" method="post">
	file:<input name="file" type="file" /><br/>	
	<!-- imgs_type:<input name="imgs_type"><br/> -->
	<input type="submit">
</form><br/><br/><br/><br/>
<hr/>
video 
<form action="http://localhost:9015/u9_app_api/videos" enctype="multipart/form-data" method="post">
	file:<input name="file" type="file" /><br/>	
	imgs_type:<input name="vio_type"><br/>
	<input type="submit">
</form> <br/>

<hr/>
8081 video 
<form action="http://192.168.10.16:8081/u9_app_api/videos" enctype="multipart/form-data" method="post">
	file:<input name="file" type="file" /><br/>	
	vio_type:<input name="vio_type"><br/>
	<input type="submit">
</form> <br/>
<button type="button" onclick="up1();">上传向服务器发送Post</button><br/><br/><br/>
支付
	<hr/>
	<form action="http://localhost:9015/u9_app_api/alipay/deposit"  method="post">
	<input name="u_id" type="text" value="1"/><br/>	
	<input name="o_no" type="text" value="19920411"/><br/>	
	<input name="o_id" type="text" value="17"/><br/>	
	<input name="o_amount" type="text" value="0.01"/><br/>	
	<input type="submit">
</form>
	<img alt="" src="http://192.168.10.16:8081/u9_app_api/img_codes?img_type=2" />
    <input id="url1" type="text" value="" />
    <button type="button" onclick="qqq();">qqqq向服务器发送Post</button><hr/><hr/><hr/><hr/><hr/><hr/>
    <button type="button" onclick="aaa();">aaa向服务器发送Post</button>
    
    <button type="button" onclick="aa1a1();">bbbb向服务器发送Post</button>
    
    
    
    <hr/>
    <form action="http://localhost:9015/u9_app_api/cart" method="post">
    	u_id:<input id="u_id" name="u_id" type="text" />
    	g_id:<input name="g_id" type="text" />
    	count:<input name="count" type="text" />
    	<input type="submit" />
    </form>
    <button type="button" onclick="add1a1();">添加购物车</button>
    <button type="button" onclick="see1a1();">查看购物车</button>
    
    <button type="button" onclick="bbbb();">向服务器发送GET</button>
<form id="editFormu"  enctype="multipart/form-data" method="post">  
    <table>  
         
        <tr>  
        <td><input type="file" name="file"/></td> 
        </tr>  
        <tr>  
        <td><input type="file" name="file"/></td>  
        </tr>
        <tr>  
        <td>imgs_type:<input name="imgs_type"></td> 
        </tr>  
        <tr>  
        <td></td>   
        </tr>  
    </table>  
</form>
    <button type="button" onclick="bound();">----|-----|-----|-----</button>
    <br /><br /><br /><br /><br /><br />
    
    <input id="file_temporaryImage"  type="file" name="file" onchange="TemporaryMedia();" /> 
    <div id="div_temporaryImage"></div><br />
    <script type="text/javascript" src="/test/jquery.form.js"></script>
    <script type="text/javascript" src="/test/jquery-1.12.2.js"></script>
    <script type="text/javascript">
    
    function bound() {
		$.ajax({
			type:"GET",//192.168.10.16:8081/u9_app_
			url:"http://localhost:9015/u9_app_api/wtApp_user?login_type=1&phone=15611589313",
			async:true,
			data:{},
			success:function (res) {
				console.log(res.data);
				if (res.status == 1) {
				
				}else if (res.status == 0) {
					 $("#remark").html("此手机号已被绑定");
				}else if (res.status == 2) {
					 $("#remark").html("有账号可以关联");
				}
			}
			
		});
	}
    function bbbbadaw2() {
			var user={u_id:1,to_uid:9,uc_id:4};
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/users_coupons",
			  type:"PUT",
			  data:JSON.stringify(user),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    function bbbbadaw3() {
			var user={u_id:1,co_id:13};
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/coupons_orders",
			  type:"PUT",
			  data:JSON.stringify(user),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    
    function a321() {
			//var us=[1,2];//192.168.10.16:8081
			var a={u_id:1,cp_id:1,co_count:2,co_amount:100,co_resource:1};
			//var file = $("#img_id").val();
			//console.log("1111111111111"+file);
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/coupons_orders",//sale_services",
			  type:"POST",
			  data:JSON.stringify(a),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    
     function bbbb111() {
			var user={u_birthday:"1990-10-10"};
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/users/1",
			  type:"PUT",
			  data:JSON.stringify(user),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
        $(function(){});
    
    var cookie = {
    set:function(key,val,time){//设置cookie方法
        var date=new Date(); //获取当前时间
        var expiresDays=time;  //将date设置为n天以后的时间
        date.setTime(date.getTime()+expiresDays*24*3600*1000); //格式化为cookie识别的时间
        document.cookie=key + "=" + escape(val) +";expires="+date.toGMTString() + "; path=/";  //设置cookie
    },
    get:function(key) {//获取cookie方法
        /*获取cookie参数*/
        var getCookie = document.cookie.replace(/[ ]/g, "");  //获取cookie,并且将获得的cookie格式化,去掉空格字符
        var arrCookie = getCookie.split(";")  //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
        var tips;  //声明变量tips
        for (var i = 0; i < arrCookie.length; i++) {   //使用for循环查找cookie中的tips变量
            var arr = arrCookie[i].split("=");   //将单条cookie用"等号"为标识,将单条cookie保存为arr数组
            if (key == arr[0]) {  //匹配变量名称,其中arr[0]是指的cookie名称,如果该条变量为tips则执行判断语句中的赋值操作
                tips = arr[1];   //将cookie的值赋给变量tips
                break;   //终止for循环遍历
            }
        }
        return unescape(tips);
    }
}
    
    
    
    
    
    function a3211() {
			//var us=[1,2];//192.168.10.16:8081
			var a={u_id:1,o_id:1,as_phone:"15611589313",as_type:1,as_content:"快给我退货",remark:"121.jpg"};
			//var file = $("#img_id").val();
			//console.log("1111111111111"+file);
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/aftersale_services",//sale_services",
			  type:"GET",
			  //data:a,
			  //dataType:"json",
			  //contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    
    
    
    function a3zza2() {
			var a={u_phone:"15611589313",u_pwd:"123456aa"};
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/users",
			  type:"POST",
			  data:JSON.stringify(a),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    
    
    
	    function TemporaryMedia() {  
		    var image = $("#file_temporaryImage").val();  
		    if ($.trim(image) == "") {  
		        alert("请选择图片!");
		        return;
		    }  
		    var f = document.createElement("form");
			document.body.appendChild(f);
			var i = document.createElement("input");
			var imag = document.getElementById("file_temporaryImage");
			f.setAttribute("enctype", "multipart/form-data");
			i.type = "hidden";
			f.appendChild(i);
			f.appendChild(imag);
			i.name = "imgs_type";
			i.value = 2;
		    $(f).ajaxSubmit({  
		            type: "POST",//提交类型  
		            dataType: "json",//返回结果格式  
		            url: 'http://192.168.10.16:8081/u9_app_api/imgs/imgupload',//请求地址  
		            success: function (data) {//请求成功后的函数  
		            	$("#div_temporaryImage").append("<img style='width:300px;height:300px;' src='" + data.data.img_url + "' /><span>临时素材</span>");  
		            }, 
		            async: true  
		       
		    }); 
		}  
	 
	
		
    function  asdasdas(){
    
    	$("#editFormu").ajaxSubmit({  
        
            async: true,
            type: "POST",//提交类型  
            dataType: "json",//返回结果格式  
            url: 'http://192.168.10.16:8081/u9_app_api/imgs/imgupload',//请求地址  
            success: function (data) {//请求成功后的函数  
            	console.log("1111111111111");
            }
       
    });
    
    
    
    	/* $.ajax({
                cache: true,
                type: "POST",
                url:'http://192.168.10.16:8081/u9_app_api/imgs/imgupload',
                data:$("#editFormu").serialize(),// 你的formid
                async: true,
                error: function(request) {
                    console.log("1111111111111");
                },
                success: function(data) {
                    console.log("1111111111111");
                }
            }); */
       /*  $("#editFormu").form('submit',{
            url:'http://192.168.10.16:8081/u9_app_api/imgs/imgupload',
            success:function(data){
               console.log("1111111111111");
            }
        }); */
    }
    
    
    function aa1a2367() {
			//var us=[1,2];//192.168.10.16:8081  
			var userEssays={o_id:1};
			var o_id=1;
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/orders",
			  type:"DELETE",
			  data:JSON.stringify(userEssays),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    
    
    function aa1a232() {
			//var us=[1,2];//192.168.10.16:8081  http://192.168.10.16:8081/u9_app_api/orders
			var userEssays={ua_name:"小小小",ua_gender:1,ua_province:"北京",ua_city:"北京",ua_area:"朝阳",ua_detail:"青年路",ua_phone:"15611589313"};
			
			$.ajax({
			  url:"http://192.168.10.16:8081/u9_app_api/users_addresses/1",
			  type:"POST",
			  data:JSON.stringify(userEssays),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    

    
    function see1a3() {
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/cart?u_id=1",
			  type:"GET",
			  beforeSend: function(request) {
                        //request.setRequestHeader("token", "1_web1505725168872keau1");
                    },
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    
    
    function see1a1() {
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/cart?u_id="+$("#u_id").val(),
			  type:"GET",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    

    
    
     function aa1a1() {
			//var us=[1,2];//192.168.10.16:8081
			var userEssays={u_id:1,ue_type:2,ue_resourceurl:"",ue_label:"3333",ue_title:"33333",ue_content:"1",ers:[{er_url:"11.jsp"},{er_url:"12.jsp"}]};
			
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/users_essays",
			  type:"POST",
			  data:JSON.stringify(userEssays),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    
    
    
    function a3zz12() {
			var a={u_name:"15611589313",u_pwd:"123456aa"};
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/users",
			  type:"POST",
			  data:JSON.stringify(a),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    

     function a3zz3() {
			var a={u_id:1,ue_id:1,ec_type:1};
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/essays_comments",
			  type:"POST",
			  data:JSON.stringify(a),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    function a3zz4() {
			var a={u_id:1,news_id:1,nc_type:1};
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/news_comments",
			  type:"POST",
			  data:JSON.stringify(a),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    
    
    function up1() {
    	var nf=new FormData($("#postForm")[1]);
		//var nf1=new FormData($("#postForm")[0]);
		$.ajax({  
		     url:"http://192.168.10.16:8081/u9_app_api/imgs",  
		     type: 'POST',  
	          data: nf,  
	          async: false,  
	          cache: false,  
	          contentType: false,  
	          processData: false, 
		     //contentType:"multipart/form-data", 
		     success: function(data) {  
		         console.log("1111111111111");
		     }  
		      
		});
	}
     function a444() {
			//var us="1,4";//192.168.10.16:8081
			var list ="12,13";
			$.ajax({
			  url:"http://192.168.10.16:8081/u9_app_api/goods_favorites?list="+list,
			  type:"GET",
			  //data:{list:"1,4"},
			  //dataType:"json",
			  //contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    

    
    function a333() {
			//var us=[1,2];//192.168.10.16:8081
			var a={u_id:1,o_no:"19920411",o_id:17,o_amount:0.01};
			//var file = $("#img_id").val();
			//console.log("1111111111111"+file);
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/alipay/deposit",
			  type:"POST",
			  data:JSON.stringify(a),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    
    
    
    
    
    function a222() {
			var us={u_id:1,ue_type:2,ue_label:"美容雾化仪",ue_title:"u9圈消息1",ue_content:"1111111111111111111",ue_resourceurl:"",ers:[{er_url:"1.jsp"},{er_url:"2.jsp"}]};//192.168.10.16:8081
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/users_essays",
			  type:"POST",
			  data:JSON.stringify(us),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
    

   
   function aaa() {
	var name =1;
	var str =1;
	var osel=8998;
	var os=[{g_id:str,g_name:"美容保湿雾化仪",g_price:osel,od_count:str,remark:"亮黑色/16G/3800万马力"}];
	
	
	var order ={u_id:name,ua_id:str,ods:[{g_id:str,g_name:"美容保湿雾化仪",g_price:osel,od_count:str,remark:"亮黑色/16G/3800万马力"}]};
		
		var us={u_phone:"15611589313",u_pwd:"123456"};
		$.ajax({
	        type:"POST",
	        dataType:"json",
	        //url:"http://192.168.10.16:8081/u9_app_api/caches?phone_nonce_str=1&phone_code=3058&phone=123131 ",
	        url:"http://localhost:9015/u9_app_api/orders",
	        data:[1,2,3,6],
	        success:function(res){
	            console.log("111111111111");
	        }
	    });
	}
		
	
	function cccccc() {
		var json ={u_id:1,ua_id:1,ods:[{g_id:1,g_name:"美容保湿雾化仪",g_price:8998,od_count:1,remark:"亮黑色/16G/3800万马力"},{g_id:2,g_name:"保湿液",g_price:98,od_count:2,remark:"亮黑色/16G/3800万马力"}]};
		$.ajax({
	        type:"POST",
	        dataType:"json",
	        //url:"http://192.168.10.16:8081/u9_app_api/caches?phone_nonce_str=1&phone_code=3058&phone=123131",
	        url:"http://192.168.10.16:8081/u9_app_api/orders",
	        data:JSON.stringify(json),
	        contentType : 'application/json;charset=utf-8', //设置请求头信息  
	        success:function(res){
	            console.log("111111111111");
	        }
	    });
	}
   
   function bbb1() {
			var order={u_id:"1",g_name:"112213",ua_id:"1",remark:"15210013953_21_Phonecode"};//192.168.10.16:8081
			$.ajax({
			  url:"http://192.168.10.16:8081/u9_app_api/orders",
			  type:"POST",
			  data:JSON.stringify(order),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log(data);
			  	if(data.status==1){
			  		console.log("222222222222222");
			  	}
			  	console.log("1111111111111");
			   }
			});
			}
			
   function bbbb() {
			var user={u_gender:0};
			$.ajax({
			  url:"http://localhost:9015/u9_app_api/users/1",
			  type:"PUT",
			  data:JSON.stringify(user),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
			  	console.log("1111111111111");
			   }
			});
	}
   
   
    function qqq() {
    	
    	var u =$("#url1").val();
    	
    	console.log(u);
		$.ajax({
                type:"get",
                url:"http://localhost:9015/u9_app_api/"+u,
                dataType:"json",
                aysn:true,
                //data:$("#f1").serialize(),
                success:function(msg){
                console.log("=============================");
                	console.log(msg);
	                if(msg.status==1){
	                	console.log(msg.data);
	                }else if(msg.status==0){
	                	console.log(msg.data);
	                }
                },
                error:function(){
                    console.log("网络连接异常");
                }
            });
	}
    
    
    
	    function pp() {
			var xhr = new XMLHttpRequest();
			var url ="http://localhost:9015/u9_app_api/api/news_comments/";
			var data = JSON.stringify({
			      	u_id:1,
					news_id:1,
					nc_type:1,
					nc_content:"ada",
					nc_parentid:1,
					remark:"ad",
					other:"asd"
			});
			
			xhr.open("POST", url );
			xhr.setRequestHeader('Content-Type','application/json');
			xhr.setRequestHeader("accept", "application/json");
			xhr.onload = function(){
			    console.log(xhr.responseText); // 这里要用xhr.respnseText 获取数据,我经常给函数传一个data参数,获取数据,造成错误
			};
			xhr.send(data);
		}
    
        var products = {
            u_id:1,
			news_id:1,
			nc_type:1,
			nc_content:"ada",
			nc_parentid:1,
			remark:"ad",
			other:"asd"
        };
        /* products.push({
            id : 2,
            name : "iPhone 7 Plus",
            price : 5987.5
        });
        products.push({
            id : 3,
            name : "iPhone 8 Plus",
            price : 6987.5
        }); */
        function addPdts_click1() {
          console.log("22222");
            $.ajax({
                type:"POST",
                //请求谓词类型
                url:"http://localhost:9015/u9_app_api/api/news_comments",
                data:JSON.stringify(products), //将products对象转换成json字符串
                contentType:"application/json;charset=UTF-8",
                //发送信息至服务器时内容编码类型,(默认: "application/x-www-form-urlencoded")
                dataType:"json", //预期服务器返回的数据类型
                success:function(result) {
                console.log("res:"+result);
                    $("#msg").html(result);
                }
            });
        }
        function addPdts_click2() {
            $.ajax({
                type : "POST",
                //请求谓词类型
                url : "http://127.0.0.1:9015/u9_app_api/api/news_comments",
                data : JSON.stringify(products), //将products对象转换成json字符串
                contentType : "application/json;charset=UTF-8",
                //发送信息至服务器时内容编码类型,(默认: "application/x-www-form-urlencoded")
                dataType : "json", //预期服务器返回的数据类型
                success : function(result) {
                    var str = {};
                    $.each(result, function(i, obj) {
                        str +=obj;
                    });
                    $("#msg").html(str);
                }
            });
        }
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/yufengaotian/article/details/82190708