ajax發送REST接口請求
form 表單
<form id="form1" action="" method="POST" onsubmit="return false;" autoscroll="true">
<li>
<strong>產品代碼 <font color="#ff0000">*</font></strong>
<div class="fl"><input type="text" name="productCode" value="" class="ipt nickname"/></div>
</li>
<li><input type="submit" value="確認提交" onclick="submitOrder()" class="submitBtn"/></li>
</form>
script
function submitOrder() {
$.ajax({
type: "POST",//方法類型
dataType: "json",//傳遞的數據類型
url: "",//調用後臺的REST接口
data: $('#form1').serialize(),//表單數據 data 傳輸後臺的參數
success: function (result) {
console.log(result);//打印服務端返回的數據(調試用)
if (result.resultCode == 200) {
alert("SUCCESS");
}
;
},
error : function() {
alert("方法返回異常!");
}
});
}
註釋:
ajax傳送json格式數據,關鍵是指定contentType
,data要是json
格式
type 類型:
post
(增)、delete
(刪)、put
(改)、get
(查)