jQuery的 - 如何使$。员额()使用的contentType =应用程序/ JSON? [英] Jquery - How to make $.post() use contentType=application/json?

查看:171
本文介绍了jQuery的 - 如何使$。员额()使用的contentType =应用程序/ JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当使用jQuery的$。员额(),默认的contentType为应用程序/ x-WWW的形式urlen codeD - 当我的asp.net mvc的code需要具备的contentType =应用程序/ JSON

I've noticed that when using $.post() in jquery that the default contentType is application/x-www-form-urlencoded - when my asp.net mvc code needs to have contentType=application/json

(见这个问题,为什么我必须使用的应用程序/ JSON:<一href="http://stackoverflow.com/questions/2792603/aspnet-mvc-why-is-modelstate-isvalid-false-the-x-field-is-required-when-that">http://stackoverflow.com/questions/2792603/aspnet-mvc-why-is-modelstate-isvalid-false-the-x-field-is-required-when-that)

(See this question for why I must use application/json: http://stackoverflow.com/questions/2792603/aspnet-mvc-why-is-modelstate-isvalid-false-the-x-field-is-required-when-that)

我怎样才能让$。员额()发送的contentType =应用程序/ JSON?我已经有大量的$。员额()函数,所以我不想更改为$阿贾克斯(),因为它会花费太多时间

How can I make $.post() send contentType=application/json? I already have a large number of $.post() functions, so I don't want to change to $.ajax() because it would take too much time

如果我尝试

$.post(url, data, function(), "json") 

它仍然具有的contentType =应用程序/ x-WWW的形式urlen codeD。那么究竟是什么的JSON参数做,如果它不改变的contentType以JSON?

It still has contentType=application/x-www-form-urlencoded. So what exactly does the "json" param do if it does not change the contenttype to json?

如果我尝试

$.ajaxSetup({
  contentType: "application/json; charset=utf-8"
});

这是工作,但会影响每一个$不用彷徨和$。员额,我已经和导致一些突破。

That works but affects every single $.get and $.post that I have and causes some to break.

那么,有没有一些方法,我可以改变$。员额()的行为发出的contentType =应用程序/ JSON?

So is there some way that I can change the behavior of $.post() to send contentType=application/json?

推荐答案

我想你可能需要

1.Modify源,使$。员额总是使用JSON数据类型,因为它真的是只为配置了pre $。阿贾克斯调用快捷键

1.Modify the source to make $.post always use JSON data type as it really is just a shortcut for a pre configured $.ajax call

2 定义您自己的效用函数是为 $。AJAX 配置要使用

3,你可以覆盖 $。POST功能通过猴子修补自己的实现。

3.You could overwrite the $.post function with your own implementation via monkey patching.

的JSON数据类型,在你的榜样是指返回的数据类型的的服务器,而不是格式的发送的服务器。

The JSON datatype in your example refers to the datatype returned from the server and not the format sent to the server.

这篇关于jQuery的 - 如何使$。员额()使用的contentType =应用程序/ JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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