Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.InvalidOperationException:缺少参数_C#_Asp.net_Ajax_Web Services - Fatal编程技术网

C# System.InvalidOperationException:缺少参数

C# System.InvalidOperationException:缺少参数,c#,asp.net,ajax,web-services,C#,Asp.net,Ajax,Web Services,我试图将参数发送到asmx(web服务文件),但收到关于“System.InvalidOperationException:缺少参数”的错误。请帮我解决这个问题,非常感谢 这是我的ajax函数 $("#dd_address").change(function () { var rowID = $(this).find(':selected').val(); console.log(rowID); $.ajax({

我试图将参数发送到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>

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

var rowID = { "id" : $(this).find(':selected').val() };
然后,在方法中按如下方式传递:-

data : JSON.stringify(rowID)

除此之外,您的ADO.NET代码可能受到攻击,因此请改用参数化查询。

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

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

ASPX页面按钮:

<input type="button" value="submit" onclick="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>
[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();
}

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