如何通过 jquery ajax 调用将参数传递给通用处理程序

发布于 2025-01-05 22:39:33 字数 358 浏览 0 评论 0原文

如何通过jquery ajax调用将参数传递给通用处理程序并且返回类型必须为json格式?

我用它来调用没有输入参数的方法。我该如何改变它?

 $.ajax
            ({

                type: "Get",
                url: "../DataPoint.ashx",
                data: "MethodName=GetPoint",
                success: function (msg) {

                    n = msg;

                }
            });

How to pass parameters to generic handlers through jquery ajax call and the return type must in json format?

I'm using this to call a method with no input parameters. How do I change it?

 $.ajax
            ({

                type: "Get",
                url: "../DataPoint.ashx",
                data: "MethodName=GetPoint",
                success: function (msg) {

                    n = msg;

                }
            });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年 2025-01-12 22:39:33

我不太明白你的问题,但这是将参数推送到返回 json 字符串的服务器函数的一种方法......

inputData={
    MethodName:"GetPoint",
    arg1:val1,
    arg2:val2,
    argN:valN
};

function success(outputData){
    var obj = jQuery.parseJSON(outputData);
}

$.ajax({
  type: 'POST',
  url: "../DataPoint.ashx",
  data: inputData,
  success: success,
  dataType: dataType
});

I don't undestarnd very well your question but this is one way to push arguments to the server function that returns a json string...

inputData={
    MethodName:"GetPoint",
    arg1:val1,
    arg2:val2,
    argN:valN
};

function success(outputData){
    var obj = jQuery.parseJSON(outputData);
}

$.ajax({
  type: 'POST',
  url: "../DataPoint.ashx",
  data: inputData,
  success: success,
  dataType: dataType
});
微暖i 2025-01-12 22:39:33

您的意思是像下面的代码示例中那样(传递参数)

$.ajax({
    type: "POST",
    url: PagePath + '/' + functionName,
    data: '{"sentData":' + JSON.stringify(parameterArray) + '}',   
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        console.log(msg.d);           
    },
    error: onFail
});

Do you mean like in the code sample below (passing parameters)

$.ajax({
    type: "POST",
    url: PagePath + '/' + functionName,
    data: '{"sentData":' + JSON.stringify(parameterArray) + '}',   
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        console.log(msg.d);           
    },
    error: onFail
});
寄人书 2025-01-12 22:39:33

我永远无法让参数传递,所以我求助于将它们传递到标头中并在处理程序中解析它们。

var args = Arg1 + "," + Arg2 + "," + Arg3;
$.ajax({
type: "POST",
url: PagePath + '/' + functionName,
data: '{}',   
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
  "X-Custom-Arguments" : args
},
success: function(msg) {
    console.log(msg.d);           
},
error: onFail
});

泽德

I could never get the arguments to pass so I resorted to passing them in the headers and parse them out in the handler.

var args = Arg1 + "," + Arg2 + "," + Arg3;
$.ajax({
type: "POST",
url: PagePath + '/' + functionName,
data: '{}',   
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
  "X-Custom-Arguments" : args
},
success: function(msg) {
    console.log(msg.d);           
},
error: onFail
});

Zed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文