I would like to pass parameters to my webservice from jquerys ajax. How can I do that?
I've already looked through a few of the related questions but couldn't find a solution that worked for me. I've tried this: jQuery AJAX parameter not being passed to MVC but I'm not using mvc so I'm sure that is why the solution isn't working. My jquery looks like this:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "CarService.svc/GetCar",
data: {CarID:117},
dataType: "json",
success: function (data) {
$("#lblCurrentTime").text("");
$("#lblCurrentTime").text(data.d.CarID);
}
});
Something is wrong with my 'data' part, correct? If leave the data: part as data: "{}" I can get my method to run (and I don't pass any parameter) but the moment I try the above out firebug tells me:
Firebug's log limit has been reached. 0 entries not shown. Preferences
POST http://localhost:64461/TimeService.svc/GetCar
POST http://localhost:64461/TimeService.svc/GetCar
500 Internal Server Error
1.12s
My Webservice looks like this:
[OperationContract]
public CarTable GetCar(int id)
{
using (var sqlc = new SqlConnection(@"sdfgsdfg"))
{
sqlc.Open();
var cmd = sqlc.CreateCommand();
cmd.CommandText = "HUGE QUERY HERE ^^";
//id = 117;
cmd.Parameters.Add("CarID", System.Data.SqlDbType.Int).Value = id;
using (var reader = cmd.ExecuteReader())
{
CarTable Cars = new CarTable();
while (reader.Read())
{
Cars.CarID = reader["CarID"].ToString();
Cars.CarName = reader["CarName"].ToString();
}
return Cars;
}
}
}
[DataContract]
public class CarTable
{
[DataMember]
public string CarID { get; set; }
[DataMember]
public string CarName { get; set; }
}
EDIT: If I change the data part to:
data: CarID=117,
I get Sys.ParameterCountException: Parameter count mismatch. [Break On This Error] {name: "format", type: String}