美文网首页
Jquery AJAX post提交json示例

Jquery AJAX post提交json示例

作者: rightchen | 来源:发表于2018-08-22 16:07 被阅读0次

下面是html请求web服务器api接口的示例,是post发送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",      //网上很多介绍加上此参数的,后来我发现不加入这个参数才会请求成功。
        data: JSON.stringify(opt),
      success: function (d) {
        console.log(d);
      }
    });
});
</script>
</html>

关于网上很多案例提示到需要添加contentType : "application/json"这个字段,尝试了很多次没有请求成功,后来发现去掉contentType : "application/json"才可以。不知道网上那么多案例为什么要添加这个参数。有了解的可以在评论区展开探讨。

相关文章

网友评论

      本文标题:Jquery AJAX post提交json示例

      本文链接:https://www.haomeiwen.com/subject/zjjyiftx.html