简体   繁体   English

System.InvalidOperationException:缺少参数

[英]System.InvalidOperationException: Missing parameter

I try to send parameter to asmx(web service file) but i get error about "System.InvalidOperationException: Missing parameter". 我尝试将参数发送到asmx(Web服务文件),但我收到有关“System.InvalidOperationException:Missing parameter”的错误。 Please help me to solve this problem and thank you so much 请帮我解决这个问题,非常感谢你

this is my ajax function 这是我的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
            });
        });

this is my code from asmx 这是我的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);
    }

and in web config file 并在Web配置文件中

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

Problem in your code is in the way you are passing the input parameter to the method, change it like this:- 您的代码中的问题在于您将输入参数传递给方法的方式,更改方式如下: -

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

Then, pass it like this in the method:- 然后,在方法中像这样传递: -

data : JSON.stringify(rowID)

Apart from this your ADO.NET code is open for SQL Injection attack, so please use parametrized query instead. 除此之外,您的ADO.NET代码对SQL注入攻击是开放的,因此请使用参数化查询。

I have updated your code, below code works for me. 我已更新您的代码,下面的代码适合我。 Please remember I have hardcoded var rowID = 20; 请记住我有硬编码var rowID = 20; make your changes as you need. 根据需要进行更改。

Please let me know if you have any questions. 请让我知道,如果你有任何问题。

ASPX Page button: ASPX页面按钮:

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

Javascript "sendrequest" Function: 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>

Webservice Method: 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM