gpt4 book ai didi

jQuery 发布。无法使用自定义 httphandler 获取请求参数

转载 作者:行者123 更新时间:2023-12-01 00:25:43 25 4
gpt4 key购买 nike

我有一个包含 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 数据发布。为什么我无法获取参数?

感谢任何帮助。

最佳答案

感谢大家的意见。我决定改为读取请求的输入流并从中获取键值对。我可以通过这种方式访问​​我的所有参数。

我还使用 $.toJSON() 函数将参数传递给 Ajax 调用。JsonConvert 类来自 Newtonsoft 的 JSON.Net 程序集。我经常使用它,如果您使用任何 json 序列化内容,强烈建议您使用它。

顺便说一句,更改输入参数周围的引号确实有效。我想继续使用一个通用的 ajax 函数并使用 $.toJSON 函数,并且通常传递一个包含所有参数的对象作为发布数据。

TextReader reader = new StreamReader(context.Request.InputStream);
Dictionary<string, string> requestParams = JsonConvert.DeserializeObject<Dictionary<string, string>>(reader.ReadToEnd());
try
{

switch (requestParams["operation"])

关于jQuery 发布。无法使用自定义 httphandler 获取请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3248526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com