There were two issues in getting this resolved.
1. Some bug in my service itself (this is my bad)
2. I had do to JSON.stringify() on the orderJson object.
function SaveOrder() {
var orderJson = {
AdditionalInstructions: $("span:first").text(),
Customer: {
FirstName: $("#firstName").val(),
LastName: $("#lastName").val()
},
OrderedProduct: {
Id: $("#productList").val(),
Quantity: $("#quantity").val()
}
};
// the post to your webservice or page
$.ajax({
type: "POST", //GET or POST or PUT or DELETE verb
url: "http://localhost:14805/OrderService.svc/SaveOrder", // Location of the service
data: JSON.stringify(orderJson), //Data sent to server
contentType: "application/json; charset=utf-8", // content type sent to server
dataType: "json", //Expected data format from server
processdata: false, //True or False
success: function (result) {//On Successfull service call
RedirectToMvcApp(result);
},
error: function (request, error) {// When Service call fails
alert('Service call failed: ' + request.status + ' ' + request.statusText);
}
});
}
Option 1. Keep your server side code the same
First remove the kendo.stringify. Then either remove the contentType or change it to...
"application/x-www-form-urlencoded; charset=utf-8"
...or change your $.ajax call to this:
$.post('DocSummaryDataAsync.aspx', { vendorId: supplierId, businessUnit: busUnit, productSegmentId: prodSegmentId, programId: progId, productManagerId: prodManagerId, companyIds: compIds, expired: exp.toString(), requestType: 'TotalCount' }, function (data) { });
Option 2. Change the POST to GET
Like this
$.ajax({
async: true,
type: "GET",
etc.
This will pass your data via the QueryString. If you remove the kendo.stringify call you would access all the values like this:
string vendorId = Request.QueryString[0];
string businessUnit = Request.QueryString[1];
etc.
Option 3. Use your original $.ajax call
If you use your original $.ajax, then the following applies:
Request.Params gets a "combined collection of QueryString, Form, Cookies, and ServerVariables items." - this link
You are not working with any of those. Instead, you need to access Request.InputStream.
Here's how you can do that:
Create a class on the server side which maps to the requested JSON object, e.g.
public class MyClass
{
// The type (int or string) should probably correspond to the JSON
public int vendorId { get; set; }
public string businessUnit { get; set; }
public string productSegmentId { get; set; }
public string programId { get; set; }
public string productManagerId { get; set; }
public string companyIds { get; set; }
public string expired { get; set; }
public string requestType { get; set; }
}
Convert Request.InputStream into that type, and then you can use it.
public void ProcessRequest()
{
System.IO.Stream body = Request.InputStream;
System.Text.Encoding encoding = Request.ContentEncoding;
System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
string json = reader.ReadToEnd();
JavaScriptSerializer serializer = new JavaScriptSerializer();
MyClass myclass = (MyClass)serializer.Deserialize(json, typeof(MyClass));
int vendorId = myclass.vendorId;
string requestType = myclass.requestType;
// etc...
}
protected void Page_Load(object sender, EventArgs e)
{
ProcessRequest();
}
Best Solution
I am not familiar with
C#
,but in your question you have addin your
Ajax
method which means the data parameter isjson
format,so you need to access asjson
object instead of access string directly.Two ways to solve it:
a. Change your
C#
controller method to access like ajson
objectb. Send
Ajax
parameter withoutjson
,code similar to below: