jQuery 发布。无法使用自定义httphandler获取请求参数
我有一个包含 JSON 数据的 jQuery post 方法。
在我的 httphandler 中,在 processRequest 方法中,Request["Operation"] 为 null,并且没有发布我的任何数据。我处于 SharePoint 2010 环境中。
public void ProcessRequest(HttpContext context)
{
try
{
string operation = context.Request["Operation"]; // Returns null
我的 JavaScript 如下:
function CallService(serviceData, callBack) {
$.ajax({
type: "POST",
url: ServiceUrl,
data: { Operation : "activate"},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
callBack(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
在 VS 的调试器中,当我评估 HttpContext 时,我找不到发布的值。在 Firebug 中,该值作为有效的 JSON 数据发布。为什么我无法获取参数?
任何帮助表示赞赏。
I have a jQuery post method with JSON data included.
In my httphandler, in the processRequest method, Request["Operation"] is null and none of my data is posted. I am in a SharePoint 2010 environment.
public void ProcessRequest(HttpContext context)
{
try
{
string operation = context.Request["Operation"]; // Returns null
My JavaScript is as follows:
function CallService(serviceData, callBack) {
$.ajax({
type: "POST",
url: ServiceUrl,
data: { Operation : "activate"},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
callBack(result);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
In the debugger in VS I can't find the posted values when I evaluate the HttpContext. In Firebug, the value is posted as valid JSON data. Any reason why I cant get the parameters?
Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢大家的意见。我决定改为读取请求的输入流并从中获取键值对。我可以通过这种方式访问我的所有参数。
我还使用 $.toJSON() 函数将参数传递给 Ajax 调用。
JsonConvert 类来自 Newtonsoft 的 JSON.Net 程序集。我经常使用它,如果您使用任何 json 序列化内容,我强烈建议您使用它。
顺便说一句,更改输入参数周围的引号确实有效。我想继续使用一个通用的 ajax 函数并使用 $.toJSON 函数,并且通常传递一个包含所有参数的对象作为发布数据。
Thanks for all your input guys. I have decided to read the input stream of the request instead and get a key value pair from that. I can access all my params that way.
I am also using the $.toJSON() function to pass my parameters to the Ajax call.
The JsonConvert class is from JSON.Net assembly from Newtonsoft. I use it a lot and would highly recommend using it if you use any json serialisation stuff.
By the way, changing the quotes around the input params did work. I want to keep using one generic ajax function and use $.toJSON function and generally pass an object with all my parameters as the post data.
我将 contentType 更改为 application/x-www-form-urlencoded ,它起到了作用
I changed contentType to
application/x-www-form-urlencoded
and it did a trick也许您受到同源政策的限制。
ServiceUrl
是否与调用页面具有相同的主机名和域?Maybe you're being restricted by the Same Origin Policy. Is
ServiceUrl
at the same hostname and domain as the calling page?为什么要覆盖
$.ajax()
调用中的contentType
选项?如果忽略这一点,您是否仍然会看到Operation
值发送 null ?另外,我认为 JSON 数据的正确格式是:
我认为 JSON 规范对此有具体规定,但大多数框架并不那么严格。
Why are you overriding the
contentType
option in your$.ajax()
call? If you omit that, do you still see null being sent in for theOperation
value?Also, I think the proper formatting for JSON data would be:
I think the JSON spec is specific about that, but most frameworks aren't as strict.