【问题标题】:System.InvalidOperationException: Missing parameterSystem.InvalidOperationException:缺少参数
【发布时间】:2015-03-31 16:10:53
【问题描述】:

我尝试将参数发送到 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);
    }

在网络配置文件中

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

【问题讨论】:

  • 这很容易受到 sql 注入的影响,您应该考虑至少将查询更改为 SqlCommand + 参数
  • 但是错误 Missing parameter 它意味着来自 ajax 的参数没有发送到 asmx。对不对?

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


【解决方案1】:

代码中的问题在于您将输入参数传递给方法的方式,将其更改为:-

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

然后,像这样在方法中传递它:-

data : JSON.stringify(rowID)

除此之外,您的 ADO.NET 代码对SQL Injection 攻击开放,因此请改用参数化查询。

【讨论】:

    【解决方案2】:

    我已经更新了你的代码,下面的代码对我有用。请记住我已经硬编码了var rowID = 20;,您可以根据需要进行更改。

    如果您有任何问题,请告诉我。

    ASPX 页面按钮:

    <input type="button" value="submit" onclick="sendrequest();" />
    

    Javascript“发送请求”功能:

    <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>
    

    网络服务方法:

    [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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 2022-01-16
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多