I am Trying to Post Json data from html page to ASP page and receive it in a function. like this:
address1 = [["123 Main St", "", "MESA", "AZ", ""],
["5088 n desert ln", "", "PRESCOTT VALLEY", "AZ", "86314"]];
address1 = JSON.stringify(address1);
$.ajax({
url: "Default2.aspx/GetPostData",
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: '{"address1":"' + address1 + '"}',
traditional : true,
success: function (data2) {
alert("success::" + data2.d);
},
error: function (response) {
alert("ERROR:::" + response.d);
}
});
and receive it in following function in asp page
[System.Web.Services.WebMethod]
public static string GetPostData(String address1)
{
------
}
But data is not receiving in json format it is received as a text As shown
123 Main St,,MESA,AZ,,5088 n desert ln,,PRESCOTT VALLEY,AZ,86314
i want to receive it as json only ..
Please suggest what wrong i am doing.
If you change your function signature to use an object, you can then convert this object into a C# model using JsonConvert. Some sample code from an ASP.NET application that I am working on :
[Route("api/inviteuser/{model}")]
[HttpPut]
public HttpResponseMessage InviteUser(Object model)
{
var jsonString = model.ToString();
InviteUserModel result = JsonConvert.DeserializeObject<InviteUserModel>(jsonString);
}
Where InviteUserModel is a simple C# object:
public class InviteUserModel
{
public string EmailAddress { get; set; }
public string Message { get; set; }
public long CurrentCompId { get; set; }
}
I found two issues here:
1- In your Ajax call the json is not valid. change it to. also make your address elements more standard.
var address1 = [{ "Street1": "123 Main St", "Street2": "", "City": "MESA", "State": "AZ", "Zip": "" }, { "Street1": "5088 n desert ln", "Street2": "", "City": "PRESCOTT VALLEY", "State": "AZ", "Zip": "86314" }];
$.ajax({
url: "http://localhost:63850/Default.aspx/GetPostData",
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: '{"address1":' + address1 + '}',
//'{"address1":"' + address1 + '"}',
traditional: true,
success: function (data2) {
alert("success::" + data2.d);
},
error: function (response) {
alert("ERROR:::" + response.d);
}
});
2- The web meothod parameter is string while the address1 is passed as array.
Asp.NET is throwing this error for this. {"Message":"Type \'System.String\' is not supported for deserialization of an array...
Create an address container class and set the web method parameter as List:
public class Address
{
public string Street1 { get; set; }
public string Street2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
}
[System.Web.Services.WebMethod]
public static string GetPostData(List<Address> addresses)
{
// addresses[0].Street1;
// addresses[0].City;
return null;
}
3- Incase you dont want to change the address javascript array.
[System.Web.Services.WebMethod]
public static string GetPostData(object address1)
{
List<string[]> list = new List<string[]>();
string[][] addresses = new string[2][];
foreach (var addressList in address1 as object[])
{
string[] addressElement = new string[5];
int index = 0;
foreach (var temp in addressList as object[])
{
addressElement[index++] = (temp != null) ? temp.ToString() : string.Empty;
}
list.Add(addressElement);
}
return null;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.