如何从JQuery传递字符串参数给web服务中的webmethod

本文关键字:web 服务 webmethod 参数 字符串 JQuery | 更新日期: 2023-09-27 18:03:57

这是我在web服务中的webmethod(本地的示例项目)。asmx

[WebMethod]
    public List<test1> GetLstB(string s)
    {
        test1 t;
        List<test1> lstB = new List<test1>();
        t = new test1()
        {
            itm1 = "aaa" + s,
            itm2 = "bbb" + s
        };
        lstB.Add(t);
        t = new test1()
        {
            itm1 = "ccc" + s,
            itm2 = "ddd" + s
        };
        lstB.Add(t);

        return lstB;
    }
public class test1
{
    public string itm1 { get; set; }
    public string itm2 { get; set; }
}

这里是调用webmethod

的JQuery
$(function () {
    $("[id*=Button4").click(function () {
        var x = 'xyz';
        $.ajax({
            type: "POST",
            url: "ServiceCS.asmx/GetLstB",
            data: "{'" + x + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",            
            success: function (result) {
                for (var i in result.d) {
                    alert(result.d[i].itm1 + "--" + result.d[i].itm2);
                }
            },
            error: function (r) {
                alert(r.responseText);
                console.log("AJAX error in request: " + JSON.stringify(r, null, 2));
            },
            failure: function (r) {
                alert(r.responseText);
            }
        });
        return false;
    });
});

我在没有传递参数的情况下运行了这个JQuery函数(var x = 'xyz';),并且webmethod返回了我在JQuery中迭代的对象列表,没有任何问题。但后来我决定尝试传递一个参数给webmethod(也尝试了"xyz"),现在我得到这个错误-我如何传递参数var x ="xyz";到webmethod正确吗?

"Message'": "传入的对象无效,'u0027:'u0027或'u0027}'u0027预期。(7): {'u0027xyz'u0027}'",'"StackTrace'":'" at System.Web.Script.Serialization.JavaScriptObjectDeserializer。deserializdictionary (Int32 depth)'r'n at system . web . script . serialize . javascriptobjectdeserializer。DeserializeInternal(Int32 depth)'r'n在system.web . script . serializ.javascriptobjectdeserializer。'r'n在System.Web.Script.Serialization.JavaScriptSerializer. js中输入一个字符串。'r'n在System.Web.Script.Serialization.JavaScriptSerializer序列化器,字符串输入,类型类型,Int32深度限制反序列化[T](字符串输入)'r'n at System.Web.Script.Services.RestHandler。GetRawParamsFromPostRequest (HttpContext上下文,JavaScriptSerializer序列化器)在System.Web.Script.Services.RestHandler ' r ' n。GetRawParams (WebServiceMethodData methodDat

如何从JQuery传递字符串参数给web服务中的webmethod

以上两个答案对于初学者来说仍然是毫无头绪的。因为javaScript和jquery是区分大小写的。

传递参数的正确语法是

data: "{'s':'"+x+"'}"

参数应该是这样的

$(function () {
    $("[id*=Button4").click(function () {
        var x = 'xyz';
        $.ajax({
            type: "POST",
            url: "ServiceCS.asmx/GetLstB",
            data: "{'s:" + x + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",            
            success: function (result) {
                for (var i in result.d) {
                    alert(result.d[i].itm1 + "--" + result.d[i].itm2);
                }
            },
            error: function (r) {
                alert(r.responseText);
                console.log("AJAX error in request: " + JSON.stringify(r, null, 2));
            },
            failure: function (r) {
                alert(r.responseText);
            }
        });
        return false;
    });
});

或者你可以这样做:

 data:JSON.stringify({ s:  x})
$(function () {
    $("[id*=Button4]").click(function () {
        var x = 'xyz';
        $.ajax({
        type: "POST",
        url: "ServiceCS.asmx/GetLstB",
        data: {s:x},
        contentType: "application/json; charset=utf-8",
        dataType: "json",            
        success: function (result) {
            for (var i in result.d) {
                alert(result.d[i].itm1 + "--" + result.d[i].itm2);
            }
        },
        error: function (r) {
            alert(r.responseText);
            console.log("AJAX error in request: " + JSON.stringify(r, null, 2));