前几天,项目需要在后台接受Posted json data。于是我做了一个简单的json发送页面如下:
页面上:
<div>
<input id="btnSentJson" type="button" value="Post Json" />
</div>
JS处理:
$.ajaxSetup({ cache: false });
$(function() {
$("#btnSentJson").click(
function() {
PostJson();
}
)
}
);
function PostJson() {
var jsont = { "pt": "app", "lg": "en", "OS": "and2.2", "pn": 9998, "CV": "0001000400040107" };
alert(JSON.stringify(jsont));
$.ajax(
{
url: "http://localhost:1470/EProduct.aspx?json=true",
type: "POST",
dataType: "json",
//processData: false,
data: JSON.stringify(jsont),//jsont,
contentType: "application/json; charset=utf-8",
success: function(theback) {
alert(JSON.stringify(theback));
},
error: function(error) {
alert(error.status + ";;" + error.responseText);
}
});
}
因为默认浏览器是firefox,所以每次都在firefox下执行。在目标程序都能接收到Post的请求,但没有内容。Request,Form,inputstream全部都是空的。我上网查了很多资料,试过了无限可能,可惜除了被误导还是被忽悠。始终没有解决问题。我尝试用firefox的Poster工具,最终实现了后台的处理:
if (Request.InputStream != null && Request.InputStream.Length > 0)
{
byte[] byts = new byte[Request.InputStream.Length];
Request.InputStream.Read(byts, 0, byts.Length);
string JsonInfo = System.Text.Encoding.Default.GetString(byts);
JsonInfo = Server.UrlDecode(JsonInfo);
JsonInput jiObj = JSONSerializer.Deserialize<JsonInput>(JsonInfo);
其中的JsonInput是自定义的一个类,定义了各个传入的参数。
但最终,我的发送程序还是不能奏效啊,百思不得其解之间,居然看到一种可能就是firefox不支持Option意外的传送方式。
type: "POST",
无论type定义成什么,最终都被解释为Option。我在后台调试的时候特意留意了一下type,果然是Option。于是将浏览器改为IE再次尝试,居然神奇的通过了。
因为这么一个firefox的问题,让我纠结了这么久。伤不起啊。