使用JQuery发布JSON并设置HTTP内容类型 - 'application / json' [英] Posting JSON using JQuery and to set HTTP content type - 'application /json'

查看:385
本文介绍了使用JQuery发布JSON并设置HTTP内容类型 - 'application / json'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery将Json数据发布到服务器。但是当我发布如下的帖子请求时,

I am using jquery to post Json data to server. However when I make a post request as below,

    $.ajax({
                type        :   'POST'  ,
                url         :   uri,
                data        :   jsonStrJson,
                contentType :   'application/json',
                success     :   successFunction
        });

即使我发布了一个json对象,http请求头内容类型也不是application / json。

The http request header content type is not "application/json" even though I posting a json object.

由于它不是applcation / json,服务器不会处理requset并返回415。

Since it is not applcation/json, the server does not process the requset and returns 415.

是有没有办法使用javascript或jquery API设置标题?

Is there a way to set the header using javascript or jquery API?

推荐答案

你能试试吗,

$.ajax({
    beforeSend: function(xhrObj){
        xhrObj.setRequestHeader("Content-Type","application/json");
        xhrObj.setRequestHeader("Accept","application/json");
    },
    type: "POST",
    url: uri,       
    data: jsonStrJson,               
    dataType: "json",
    success: function(json){
       console.log(json);
    }
});

这篇关于使用JQuery发布JSON并设置HTTP内容类型 - 'application / json'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆