当我从 jQuery 向 WebMethod 发送参数时出现此错误
{“消息”:“网络服务调用无效,缺少参数值:\u0027personelName\u0027。”,“StackTrace”:“在 System.Web.Script.Services.WebServiceMethodData.CallMethod(对象目标, IDictionary2 参数)\r\n 在 System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(对象目标,IDictionary
2 参数)\r\n 在 System.Web.Script.Services.RestHandler。 InvokeMethod(HttpContext 上下文,WebServiceMethodData 方法数据,IDictionary`2 rawParams)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext 上下文,WebServiceMethodData 方法数据)","ExceptionType":"System.InvalidOperationException"}
我的aspx页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="denemeAutoComp.aspx.cs" Inherits="AutoCompleteDeneme.denemeAutoComp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<link href="Style/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css" />
<script src="Script/jquery-1.10.2.js" type="text/javascript"></script>
<script src="Script/jquery-ui-1.10.4.custom.min.js" type="text/javascript">
<script type="text/javascript">
$(document).ready(function () {
$('#txtPersonelName').autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/PersonelService.asmx/GetPersonelNames") %>',
data: "{ 'searchTerm': '" + request.term + "' }",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
response(result.d);
},
error: function (result) {
alert('there is a problem processing your request');
alert(result.responseText);
}
});
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family: Arial" class="divAuto">
Name:
<asp:TextBox ID="txtPersonelName" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<br />
<asp:GridView ID="gvPersonels" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
我的网络服务(PersonelService.asmx)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Services;
namespace AutoCompleteDeneme
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class PersonelService : System.Web.Services.WebService
{
[WebMethod]
public List<string> GetPersonelNames(string personelName)
{
string CS = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
List<string> personelNames = new List<string>();
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spGetMatchingPersonelNames", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = new SqlParameter("@PersonelName", personelName);
cmd.Parameters.Add(parameter);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
personelNames.Add(rdr["adi"].ToString());
}
}
return personelNames;
}
}
}
最佳答案
使用 JSON 时,所有字符串都必须用双引号 "
括起来,而不是单引号 '
。\u0027
是单引号,并且可能是 API 所提示的。因此,如果您替换
data: "{ 'searchTerm': '"+ request.term + "' }",
与
data: '{ "searchTerm": "' + request.term + '"}',
它可能会起作用。
关于javascript - "Message": "Invalid web service call, 参数缺失值:\u0027,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30597028/