系统无效操作异常:缺少参数。

3

我尝试向asmx(web服务文件)发送参数,但是出现了“System.InvalidOperationException:缺少参数”的错误。请帮助我解决这个问题,非常感谢。

这是我的ajax函数:

$("#dd_address").change(function () {

            var rowID = $(this).find(':selected').val();
            console.log(rowID);
            $.ajax({
                url: "WebService.asmx/queryCity",

                data: {
                    id: JSON.stringify(rowID),                        
                },
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset-utf-8",
                success: OnSuccess,
                error: OnError
            });
        });

这是我的来自asmx的代码。
 DataTable result;

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string queryCity(string id)
    {

        DataTable dt;
        SqlConnection MRK_Conn = new SqlConnection(@"Data Source=192.168.24.30;Initial Catalog=Marketing_Data;Persist Security info=True;User ID=sa;Password=sa");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader sql_dr;
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

        MRK_Conn.Open();
        cmd = new SqlCommand("select [City RowID], [City Description] from City where [Des Ref Province] = '" + id + "'", MRK_Conn);
        dt = new DataTable();
        sql_dr = cmd.ExecuteReader();
        dt.Load(sql_dr);
        sql_dr.Close();
        MRK_Conn.Close();
        result = dt;

        return serializer.Serialize(result);
    }

在 web 配置文件中

  <webServices>
    <protocols>
      <add name="HttpGet"/>
      <add name="HttpPost"/>
    </protocols>
  </webServices>

1
这个存在 SQL 注入漏洞,你应该考虑将查询更改为 SqlCommand + 参数。 - Francis Nepomuceno
但是“缺少参数”错误意味着Ajax中的参数没有发送到asmx。对吗? - user3001046
2个回答

1

你的代码问题在于你传递输入参数的方式,将其更改为以下方式:

var rowID = { "id" : $(this).find(':selected').val() };

然后,在方法中像这样传递它:-
data : JSON.stringify(rowID)

除此之外,您的ADO.NET代码容易受到SQL注入攻击,请使用参数化查询。

1
我已经更新了你的代码,以下代码对我有效。请记住我已经硬编码了var rowID = 20;,根据需要进行更改。
如果你有任何问题,请告诉我。 ASPX页面按钮:
<input type="button" value="submit" onclick="sendrequest();" />

JavaScript "sendrequest"函数:

发送请求:

<script>
    function sendrequest()
    {
        var rowID = 20;
        console.log(rowID);
        $.ajax({
            url: "WebService.asmx/queryCity",
            data: '{"id":"' + rowID + '"}',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset-utf-8",
            success: function (data) {
                var xmlDoc = $.parseXML(data.d);
                var xml = $(xmlDoc);
                var city = xml.find("Table1");
                alert(city.text());
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('error: ' + xhr.status + ' ' + thrownError);
            }
        });

    }
</script>

Web服务方法:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string queryCity(string id)
{
    DataSet ds = new DataSet();
    DataTable dt= new DataTable("Table");
    SqlConnection MRK_Conn = new SqlConnection(@"Data Source=KEVAL;Initial Catalog=SampleDatabase;Persist Security info=True;User ID=sa;Password=sa123");
    SqlCommand cmd = new SqlCommand();
    SqlDataReader sql_dr;
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

    MRK_Conn.Open();
    cmd = new SqlCommand("select [City RowID], [City Description] from City where [Des Ref Province] = '" + id + "'", MRK_Conn);
    dt = new DataTable();
    sql_dr = cmd.ExecuteReader();
    dt.Load(sql_dr);
    sql_dr.Close();
    MRK_Conn.Close();
    ds.Tables.Add(dt);

    return ds.GetXml();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接