Reputation: 316
I got this error when i send a parameter from jQuery to WebMethod
{"Message":"Invalid web service call, missing value for parameter: \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, IDictionary
2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
My aspx page
<%@ 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>
My Web Service(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;
}
}
}
Upvotes: 1
Views: 15265
Reputation: 288
You should be passing the same method parameter from GetPersonelNames, in your Ajax call
data: "{ 'personelName': '" + "your data" + "' }"
Upvotes: 3
Reputation: 1476
To solve this error you must make sure that:
1. the parameter sent to the function has the same name as the param the function gets.
2.the parameter sent to the function must have the same "type" the function gets.
3. json.stringify(param)
will be only on the right side of json expression.
Code example simplify:
//function
[WebMethod]
public static doSomethimg(string[] myParam){}
//data in jquery make sure the param is an array (in this case)
data:{myParam:json.stringify(myArr)},
Upvotes: 3
Reputation: 316
it is done with a change !
Instead of
data: "{ 'searchTerm': '" + request.term + "' }",
This code works
data: JSON.stringify({
personelName : request.term
}),
Upvotes: 0
Reputation: 2376
When using JSON, all strings have to be enclosed in double quotes "
, not single quotes '
. \u0027
is a single quote, and is probably what the API is complaining about. So if you replace
data: "{ 'searchTerm': '" + request.term + "' }",
with
data: '{ "searchTerm": "' + request.term + '" }',
It might work.
Upvotes: 4