【问题标题】:"Message" : "Invalid web service call, missing value for parameter: \u0027“消息”:“无效的 Web 服务调用,参数缺失值:\u0027
【发布时间】:2015-06-02 13:06:56
【问题描述】:

当我将参数从 jQuery 发送到 WebMethod 时出现此错误

{"Message":"无效的 Web 服务调用,缺少参数值:\u0027personelName\u0027。","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary2 个参数)\r\n 在 System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n 在 System.Web.Script.Services.RestHandler。 ExecuteWebServiceCall(HttpContext context, 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>

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


    }
}
}

【问题讨论】:

    标签: javascript jquery asp.net autocomplete


    【解决方案1】:

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

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

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

    它可能会起作用。

    【讨论】:

    • 不幸的是它没有工作:(可能还有更多错误:(
    • 我已经启发了您的建议并找到了解决方案,谢谢!
    【解决方案2】:

    要解决此错误,您必须确保: 1. 发送给函数的参数与函数获取的参数同名。 2.发送给函数的参数必须与函数获取的“类型”相同。 3.json.stringify(param)只会出现在json表达式的右边。

    代码示例简化:

    //功能

     [WebMethod]
    
    public static doSomethimg(string[] myParam){}
    

    //jquery中的数据 使确保参数是一个数组(在这种情况下)

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

    【讨论】:

    • +!,如果我们将模型作为参数传递,它甚至适用。
    【解决方案3】:

    您应该在 Ajax 调用中从 GetPersonelNames 传递相同的方法参数

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

    【讨论】:

    • ... 和@Bjorn Roberg 对引号是正确的。 [使用 JSON 时,所有字符串都必须用双引号括起来,而不是单引号 '] ASP.NET 在解析 JSON 时(有时)会容忍单引号,但它们不正确。
    【解决方案4】:

    它是通过改变完成的!

    代替

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

    此代码有效

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

    【讨论】:

      猜你喜欢
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 2011-09-22
      相关资源
      最近更新 更多