ajax的dataType和contentType

ajax的contentType:

          contentType默认为:application/x-www-form-urlencoded

                                     get请求:如: wwwh.baidu.com/q?key=fdsa&lang=zh

                                     post 请求则是使用请求体,参数不在 url 中,在请求体中的参数表现形式也是: key=fdsa&lang=zh的形式。 

         问题:    只能处理简单的JSON,形式如:{a:1,b:2}, 若为

                        { data: { a: [{ x: 2 }] } }这种json套数组的就不行了。

         解决方案:contentType设置为application/json

                          data 用JSON.stringfy处理一下,服务器端接到然后用 JSON.parse 进行还原。

        $.ajax({ dataType: 'json', contentType: 'application/json', data: JSON.stringify({a: [{b:1, a:1}]}) })     

ajax的dataType和contentType

         contentType 主要设置你发送给服务器的格式,dataType设置你收到服务器数据的格式

               

猜你喜欢

转载自blog.csdn.net/weixin_42153372/article/details/82493125
今日推荐