ajax 使用get post delete put 请求restful风格接口
restfful风格:ajax请求的时候会请求两次,第一是验证请求是否符合规定的规则(百度说是对header头进行验证),请求成功返回200后再做ajax操作,个人理解
put方式:
function modifyRepairs(user,pass){
var ret;
$.ajax({
url:"http://"+ apiurl +"/api/Modify/ModifyUser",
type:"PUT",
data:JSON.stringify(username:user,password:pass),
dataType:"json",
contentType:"application/json; charset=utf-8",
async:false,
success:function(data){
//console.log(data.data)
ret = data;
//console.log(data)
}
});
return ret;
}
delete:通过URL传参,使用ajax怎么试怎么报错,后来不用ajax用xml试了一下,就成功了
var url = "http://"+ apiurl +"/api/Repair/DeleteRepair?id="+id+"";
var xhr = new XMLHttpRequest();
xhr.open('delete',url,false);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //必须写在open和send中间
xhr.send();
xhr.onreadystatechange=function(data)
{
console.log(data.responseText);
};
get方式:
function manualPlan(pages,count,text){
var ret;
$.ajax({
url:"http://"+apiurl+"/api/manualPlan/getPlineTime",
header:"Content-Type: application/json",
data:{"page":pages,"per":count,"plinenub":text},
type:"GET",
dataType:"json",
async:false,
success:function(data){
ret = data
}
});
return ret;
}
post方式:
function removePlineTime(no,time_s,time_e,liine_nub,plan_date){
var ret;
var result = {timeList:[{ "no":no, "time_s": time_s, "time_e": time_e,"pline_nub": liine_nub, "plan_date": plan_date }]};
$.ajax({
url:"http://"+apiurl+"/api/manualPlan/removePlineTime",
data:JSON.stringify(result),
type:"POST",
contentType: 'application/json;charset=utf-8',
dataType:"json",
async:false,
success:function(data){
ret = data
}
});
return ret;
}