如何将参数从原型传递到web方法

How to pass parameter to web method from prototype

本文关键字:web 方法 原型 参数      更新时间:2023-09-26

我想将参数传递给webmethod,但没有任何操作。我从方法和原型ajax请求中删除了参数,一切都很好,但当我想使用参数时,它就不起作用了。这是我的代码:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>
<script>
    var xRequest = new Ajax.Request('PrototypeTest.aspx/Test', {
        method: 'post',
        parameters: { "id": 'asdf' },
        contentType: 'application/json; charset=utf-8',
        onSuccess: function (val) {
            var brands = val.responseText.evalJSON().d.evalJSON();
            brands.each(function (brand) {
                alert(brand.Name);
            });
        },
        onerror: function (val) {
            debugger;
            alert('hata');
        }
    });
</script>
 [WebMethod]
    public static string Test(string id)
    {
        List<brand> brands = new List<brand>();
        brands.Add(new brand()
            {
                Name = "BMW",
                IsActive = true
            });
        var json = new JavaScriptSerializer();
        return json.Serialize(brands);
    }

我的错误在哪里?

我不知道这是否正确,但它解决了我的问题:

 Ajax.Request('PrototypeTest.aspx/Test?prod=1', {`...

我将参数作为查询字符串传递。