“消息”:\“无效的Web服务调用,进行参数缺失值:\\ u0027(“Message” : \"I

2019-10-23 06:16发布

我得到这个错误,当我从jQuery的发送参数的WebMethod

{ “消息”: “无效的web服务调用,用于参数缺失值:\ u0027personelName \ u0027。”, “堆栈跟踪”:”在System.Web.Script.Services.WebServiceMethodData.CallMethod(对象目标,IDictionary的2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary 2个参数)\ r \ n在System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext的上下文中,WebServiceMethodData methodData,IDictionary`2 rawParams) \ r \ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext的上下文中,WebServiceMethodData methodData)”, “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>

我的Web服务(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;


    }
}
}

Answer 1:

当使用JSON,所有的字符串必须用双引号" ,而不是单引号'\u0027是单引号,可能是什么API抱怨。所以,如果你更换

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

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

它可能会奏效。



Answer 2:

为了解决这个错误,您必须确保:1,发送给函数的参数具有相同的名称作为帕拉姆的功能得到。 发送给函数的参数2.必须具有相同的“类型”的功能得到。 3. json.stringify(param)将仅在JSON表达式的右侧。

代码示例简化:

//功能

 [WebMethod]

public static doSomethimg(string[] myParam){}

//在jquery的数据确认参数是一个阵列(在这种情况下)

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


Answer 3:

你应该从GetPersonelNames路过同样的方法参数,在你的Ajax调用

data: "{ 'personelName': '" + "your data" + "' }"


Answer 4:

它与变化做!

代替

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

此代码的工作

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


文章来源: “Message” : \"Invalid web service call, missing value for parameter: \\u0027