如何在JQuery的Web服务中将字符串参数传递给webmethod

时间:2016-08-25 21:45:08

标签: c# jquery asp.net web-services

以下是我在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;
    });
});

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

“Message \”:\“传入无效的对象,\ u0027:\ u0027或\ u0027} \ u0027 expected。(7):{\ u0027xyz \ u0027} \”,\“StackTrace \”:\“at System.Web.Script.Serialization.JavaScriptObjectDeserializer中的System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32深度)\ r \ n的System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32深度)\ r \ n。 System.Web.Script上的System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer序列化程序,字符串输入,类型类型,Int32 depthLimit)\ r \ n中的BasicDeserialize(字符串输入,Int32 depthLimit,JavaScriptSerializer序列化程序)\ r \ n .Serialization.JavaScriptSerializer.Deserialize [T](字符串输入)\ r \ n在System.Web.Script.Script.Script.Services.RestHandler上的System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext上下文,JavaScriptSerializer序列化程序)\ r \ n .GetRawParams(WebServiceMethodData methodDat

3 个答案:

答案 0 :(得分:1)

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

传递争论的正确语法是

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

答案 1 :(得分:0)

$(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));

答案 2 :(得分:0)

参数应该是这样的。

$(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})