Handle selection sent with $ .ajax (jquery) to webservice
I am trying to post data to a webservice (asp.net 3.5) as shown below (two options, one comment):
var array = [3, 2, 5, 1, 7];
var jsonString = JSON.stringify(array);
//var jsonString = '{ "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" }, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }'
$.ajax({
type: "POST",
url: "WebService2.asmx/AddRoute",
data: jsonString,
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: "false",
error: function(msg) {
alert('error' + msg.toString);
}
});
So I need a corresponding web method to get it. Something like that:
[WebMethod]
public string AddRoute(/* xxx */)
{
//handle data
}
Can anyone comment on how I can get the data where I typed "xxx"? I would have thought that "int [] array" would do the trick, but it doesn't work. Any help would be greatly appreciated :)
a source to share
I'm not familiar with ASP.NET, but there must be some way to get the request parameters that are passed using jQuery.ajax()
.
$.ajax({
type: "POST",
url: "WebService2.asmx/AddRoute",
**data: {'some_data': jsonString},**
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: "false",
error: function(msg) {
alert('error' + msg.toString);
}
});
The request parameter will be some_data. From your controller, you can get a request parameter without passing it as an argument to your function.
I apologize for being vague as I am coming from a Python / Pylons background.
a source to share
Asmx webservice, you should just expose the asp.net mvc action method. This post by Phil Haack shows how easy it is to accept strongly typed json data (including arrays):
http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action- method-argument.aspx
a source to share
Try something like the following code:
You can use int [] instead of string [], and
[WebInvoke(UriTemplate = "ServiceName", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,
Method = "POST")]
public string ServiceName(string[] ids)
a source to share
Declare your service class like this
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[System.Web.Script.Services.ScriptService]
public class WFSrvc
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public string HelloWorld()
{
return "Hello World";
}
}
instead of helloworld you can put your own function
a source to share
your ajax post should be something like this
$.ajax({
type: 'POST',
url: 'WebService2.asmx/AddRoute',
data: '{arr:'+ JSON.stringify(array)+'}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
console.log(msg.d);
}
});
and your web method
[WebMethod]
public string AddRoute(int[] arr) //parameter name and the name in data:{} should be same
{
//handle data
}
a source to share