Mohammed Alasa'ad Mohammed Alasa'ad - 22 days ago 7
Ajax Question

Ajax post return error

When I post to my asmx web service, this error is returned:


Could not create type 'myProjectName.AutoCompleteWebService'.


I tried all answers in StackOverflow and many other sites, but none one working in my case.

JQuery

$("#txtSearchKeyWord").autocomplete({
source: function (request, response) {
$.ajax({
url: "AutoCompleteWebService.asmx/IndentifySearch",
data: "{ 'keyWord': '" + request.term + "','Lang': 'En' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
$(currentElement).css({ "background": "rgb(255, 255, 255) none repeat scroll 0% 0% / auto padding-box border-box" });
response($.map(data.d, function (item) {
return {
value: item
}
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
open: function (event, ui) {
//$(".ui-autocomplete").css("left", (parseInt($(".ui-autocomplete").css("left").replace("px", "")) - 114).toString() + "px");
},
minLength: 3,
appendTo: "#Autocontainer"
});


ASMX

<%@ WebService Language="C#" CodeBehind="AutoCompleteWebService.asmx.cs" Class="RoyalTours.AutoCompleteWebService" %>


C#

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]

public class autocomp : System.Web.Services.WebService
{
[WebMethod]
public List<string> IndentifySearch(string keyWord)
{
string currentPageURL = HttpContext.Current.Request.Url.AbsolutePath;
List<Package> olstPackage = null;
olstPackage = new PackageRepository().SearchPackage(keyWord);
List<string> olstResult = null;
if (olstPackage.Count > 0)
{
olstResult = new List<string>();
for (int indexCountry = 0; indexCountry < olstPackage.Count; indexCountry++)
{
olstResult.Add(olstPackage[indexCountry].KeyWord);

if (olstPackage.Count <= 0)
{
olstResult.RemoveAt(indexCountry);
}
}
}
return olstResult;
}

}

Answer

I'd venture to guess that this is an issue of a class mismatch between your .asmx and .asmx.cs files.

On the top of your .asmx page, be sure that your Class attribute matches the class at the top of your .asmx.cs page, like so:

Code-Behind

public class myService: System.Web.Services.WebService

Front end

<%@ WebService Language="C#" CodeBehind="myService.asmx.cs" Class="MyProject.MyService" %>

It's fairly common habit if using Visual Studio to create a new Web Service (which has a default name of something like Service1.asmx), and then rename it to MyService.asmx. But it's important to note that this does not update the class references along with the filename, and so it can cause a headache in the event you forget.


EDIT: Went ahead and edited your question to clean up the code, only to realize you had actually included the markup for the page AND the code-behind.

Just as I suspected, your classes don't match. Take a look:

.asmx

Class="RoyalTours.AutoCompleteWebService"

.asmx.cs

public class autocomp
Comments