System.InvalidOperationException:缺少参数 [英] System.InvalidOperationException: Missing parameter
本文介绍了System.InvalidOperationException:缺少参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试将参数发送到asmx(Web服务文件),但出现有关"System.InvalidOperationException:缺少参数"的错误.请帮助我解决这个问题,非常感谢
I try to send parameter to asmx(web service file) but i get error about "System.InvalidOperationException: Missing parameter". Please help me to solve this problem and thank you so much
这是我的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的代码
this is my code from 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 = 20;
根据需要进行更改.
I have updated your code, below code works for me. Please remember I have hardcoded var rowID = 20;
make your changes as you need.
如果您有任何疑问,请告诉我.
Please let me know if you have any questions.
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>
网络服务方法:
[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();
}
这篇关于System.InvalidOperationException:缺少参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文