问题描述
我想一些数据发送到一个通用的处理程序,并使用jQuery.ajax传递信息,早在响应()。出于某种原因,当我发送数据了基于回答另一个问题,我发现(的)提交,没有任何的context.Request对象。
I am trying to send some data to a generic handler and pass that information back in the response using jQuery.ajax(). For some reason, when I send the data up based on an answer to another question I found (.NET Simple Form Submit via AJAX and JQUERY), there is nothing in the context.Request object.
下面是我的ajax调用:
Here is my ajax call:
function retrieveStats(monster) {
$.ajax({
type: "POST",
url: "MonsterRequests.ashx",
data: { "monster": monster },
contentType: "application/json; charset=utf-8",
success: function (msg) {
alert(msg.d);
},
error: function (jqXhr, status, errorThrown) {
alert(errorThrown);
}
});
}
这里是$ C $下我的处理程序:
And here is the code for my handler:
public class MonsterRequests : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string monsterName = context.Request["monster"];
context.Response.ContentType = "text/plain";
context.Response.Write("{\"d\":\"" + monsterName + "\"}");
}
public bool IsReusable {
get {
return false;
}
}
}
我能够通过访问context.Request.InputStream,并通过一个StreamReader读取拉断的信息,但我很好奇,为什么我不能直接拉断的信息的要求。
I am able to pull the information off by accessing the context.Request.InputStream and reading via a StreamReader, but I am curious as to why I can't just pull the info directly off of the request.
任何帮助,这将大大AP preciated。
Any help with this would be greatly appreciated.
推荐答案
删除内容类型,你是不是送JSON。试试这个。
Remove content type as you are not sending json. Try this.
var monster = "value";
$.ajax({
type: "POST",
url: "MyHandler.ashx",
data: { monster: monster },
success: function(msg) {
alert(msg.d);
},
error: function(jqXhr, status, errorThrown) {
alert(errorThrown);
}
});
这篇关于AJAX请求数据不是present的HttpContext对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!