"消息" :"无效的Web服务调用,缺少参数值:\ u0027

时间:2015-06-02 13:06:56

标签: javascript jquery asp.net autocomplete

当我从jQuery向WebMethod

发送参数时出现此错误

{"消息":"无效的Web服务调用,缺少参数值:\ u0027personelName \ u0027。"," StackTrace":& #34; at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target,IDictionary $Epsiodes 2参数)\ r \ n在System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context,WebServiceMethodData methodData,IDictionary`) 2 rawRarams)\ r \ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMethodData methodData)"," ExceptionType":" System.InvalidOperationException" }

我的aspx页面

2 parameters)\r\n   at    System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary

我的网络服务(PersonelService.asmx)

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

4 个答案:

答案 0 :(得分:3)

使用JSON时,所有字符串都必须用双引号"括起来,而不是单引号'\u0027是单引号,可能是API抱怨的内容。所以如果你替换

data: "{ 'searchTerm': '" + request.term + "' }",

data: '{ "searchTerm": "' + request.term + '" }',

它可能有用。

答案 1 :(得分:1)

要解决此错误,您必须确保: 1.发送给函数的参数与函数获取的参数名称相同。 2.发送到函数的参数必须具有相同的&#34;类型&#34;功能得到。 3. json.stringify(param)将仅位于json表达式的右侧。

代码示例简化:

//功能

 [WebMethod]

public static doSomethimg(string[] myParam){}

// jquery中的数据 make 确保param是一个数组(在本例中)

data:{myParam:json.stringify(myArr)},

答案 2 :(得分:1)

您应该在Ajax调用

中从GetPersonelNames传递相同的方法参数
data: "{ 'personelName': '" + "your data" + "' }"

答案 3 :(得分:0)

完成了改变!

而不是

 data: "{ 'searchTerm': '" + request.term + "' }",

此代码有效

data: JSON.stringify({
                        personelName : request.term
                    }),