下面是html請求web服務器api接口的示例,是post發(fā)送json方式請求。
<html>
<script src="jquery-1.7.2.min.js"></script>
<body>
</body>
<script>
$(document).ready(function(){
var opt={"app_name":"王者榮耀","nickname":"純純的1907","add_time":"2018-08-01"};
$.ajax({
type: "post",
url: "http://yours_url",
dataType : "json",
//contentType : "application/json", //網上很多介紹加上此參數(shù)的塘慕,后來我發(fā)現(xiàn)不加入這個參數(shù)才會請求成功奥秆。
data: JSON.stringify(opt),
success: function (d) {
console.log(d);
}
});
});
</script>
</html>
關于網上很多案例提示到需要添加contentType : "application/json"這個字段只锻,嘗試了很多次沒有請求成功,后來發(fā)現(xiàn)去掉contentType : "application/json"才可以摄欲。不知道網上那么多案例為什么要添加這個參數(shù)。有了解的可以在評論區(qū)展開探討疮薇。