Click here to Skip to main content
15,885,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
what is the difference between consuming a webservice and calling aspx file.
In this exemple when calling an Default2.aspx file it is working but when consuming a webservice CarService.asmx it is raising an error:
Unknown web method SaveData. Parameter name: methodName
As u can see I have comment it out the calling of the webservice var query = "CarService.asmx/SaveData";because it it raising the error mentioned above when uncomment.

This is my code:

$(function () {
var checkin = $("#datepickercheckin").val();
var checkout = $("#datepickercheckout").val();
$('#btnSearchAvailRoom').on("click", function () {
Greating();
});
 
});
function Greating() {

var query = "Default2.aspx/SaveData";
//var query = "CarService.asmx/SaveData";
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: query,
data: '{ checkindate: "'+ checkin + '", checkoutdate: "'+ checkout +'" }',
dataType: "json",
success: function (data) {
$('#divtxt').text(data.d);
},
error: function (result) {
alert("error");
}
});
}


and I am using the same code for both files webservice or the aspx.cs file, in the code behind:

C#
[WebMethod]
public static string SaveData(string checkindate, string checkoutdate)
{
string str = checkindate + " " + checkoutdate;
return str;
}


can u tell me the difference and Why I am getting that error and how to resolve it
Posted

1 solution

Can you please try to add the following to your web service

Add the below namespaces

using System.Web.Script.Services;
Add the following attribute to the web method

[System.Web.Script.Services.ScriptService]

I don't know about the exact code you are using ,but you need to return json string from the webmethod.
 
Share this answer
 
Comments
El Dev 17-Nov-14 0:38am    
I ashish_shukla,

It is not working, this is my full code:

///
/// Summary description for CarService
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 CarService : System.Web.Services.WebService {

public CarService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}
public class product {
public string name { get; set; }
public string img { get; set; }
public string descr { get; set; }
public string Checkin { get; set; }
public string Checkout { get; set; }

}

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static string SaveData(string checkindate, string checkoutdate)
{
string str = checkindate + " " + checkoutdate;
//return str;
var serializer = new JavaScriptSerializer();
return serializer.Serialize(str);

}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900