Reputation: 641
I want to pass json object to a [WebMethod].
My [WebMethod] looks like this;
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void UpdateBooksOrder(Success succ)
{
try
{
if (succ != null)
{
updateDal.LogSGDetails(succ);
}
}
catch (Exception ex)
{
logger.Error("exception ", ex);
}
}
And, I get [WebMethod] URL as;
http://localhost:50596/OrderStatusUpdate.asmx?op=UpdateBooksOrder
For testing, I am passing a json object to above [WebMethod] using html+ajax like this;
<script type="text/javascript">
$("#btnUpdate").live("click", function () {
//alert("OK");
var succ = {};
succ.id = "1";
succ.refrerence = "148997";
succ.external_ref = "GF0000148997";
succ.status = "1";
succ.status_name = "test";
$.ajax({
type: 'POST',
url: 'http://localhost:50596/OrderStatusUpdate.asmx?op=UpdateBooksOrder',
data: "{succ:" + JSON.stringify(succ) + "}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function () {
alert("OK");
}
});
});
</script>
When I run the WebService project and call it via html I get following error;
Status Code:405 Method Not Allowed
Please guide me a way to resolve that.
Upvotes: 1
Views: 2054
Reputation: 538
I did this in below way. It works fine. Web service
[WebMethod]
public string OrderstatusUpdate(OrderStatus orderStatus)
{
//do what ever
return "Success";
}
OrderStatus Class
[Serializable]
public class OrderStatus
{
public int Id { get; set; }
public string Reference { get; set; }
}
Java script
function resolveObject(data) {
if (!data.hasOwnProperty('d')) return data;
else return data.d;
}
$.ajaxSetup({ "contentType": "application/json;charset=utf-8", "dataType": "json", "error": function (e) { console.log(e); return; } });
function saveOrder() {
var a = { orderStatus: {} };
a.orderStatus.Id = 1;
a.orderStatus.Reference = "reference";
$.ajax({
type: "POST",
url: "../services/OrderService.asmx/OrderstatusUpdate",
data: JSON.stringify(a),
success: function (r) {
alert(resolveObject(r));
}
});
}
And make sure you have uncommented the following line right before the Web service class
[System.Web.Script.Services.ScriptService]
Upvotes: 1
Reputation: 6649
The [WebMethod] attribute is typically for older xml web services.
What type of project is this? If this is a newer project, please look into using the newer constructs like [HttpPost]. Can you post up the request to further investigation (using fiddler)?
Upvotes: 1