如何将json字符串传递给webmethod c#ASP.NET
我试图将javascript对象字符串化,然后将字符串作为参数传递给Code Behind中的WebMethod。 我无法让它工作,因为我得到500的内部服务器错误,堆栈跟踪说参数缺少值。
这是javascript代码:
var jSon = JSON.stringify(javascriptObject); // "{"Foretagsnamn":"Avector","BGFarg":"000000","TextColor":"fafafa","FooterFarg":"ffffff","FooterColor":"000000","FooterLinkColor":"050505","FeaturedBorderColor":"","HoverFarg":"12ebeb","RutFarg":"0d0d0d","SelectedRutFarg":"","RutColor":"FFFFFF","LankColor":"","DelaMedSig":"1","PersonalSida":"0","StartpageTitle":"","StartpageDescription":"","GoogleMaps":"
Visa större karta","HittaKartaUrl":"http://www.hitta.se/avector ab/ängelholm/hxTP-4v1HG?vad=Avector AB","EniroKartaUrl":"http://kartor.eniro.se/m/aKkhi","Ikoner":"2","Email":"info@avector.com","AdressSida":"1","shadowColor":"ffffff","lineColor":"2b292b","MenuHoverIcon":"Välj bild från server","fontFamily":"Verdana","supportText":"Support Avector","captcha":true,"metaKeywords":"","ShowSupportInFooter":true}" $.ajax({ type: "POST", url: "Post/Installningar.aspx/Updatera", data: jSon, contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { var resultAsString = result.d; //_this.parent().siblings('.SavedStatus').html(resultAsString); if (resultAsString == "1") { // Gick bra att spara. alert("Uppgifterna är sparade."); document.location = document.location; } else { $('#StatusText').html("Gick inte att spara uppgifterna."); } }, error: function (xhr, ajaxOptions, thrownError) { } });
这是网络方法:
[WebMethod] public static string Updatera(string jSon) {
感觉就像我在谷歌和SO搜索时所尝试的一切。
我也尝试过很多人提到的指南: http : //encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
有任何想法吗?
首先你需要使用
var jSon = JSON.stringify({obj:javascriptObject});
代替
var jSon = JSON.stringify(javascriptObject);
然后你的webmethod会是这样的
[WebMethod] public static string Updatera(aData obj) { // logic code }
现在这里aData就是你的类,如下所示
public class aData { public string Foretagsnamn {get;set;} public string BGFarg {get;set;} public string TextColor {get;set;} public string FooterFarg {get;set;} public string Email {get;set;} }
所以你的最终代码看起来像
jQuery:
var jSon = JSON.stringify({obj:javascriptObject}); $.ajax({ type: "POST", url: "Post/Installningar.aspx/Updatera", data: jsonData, contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, error: OnErrorCall }); function OnSuccess(response){ } function OnErrorCall(){ }
代码背后:
public class aData { public string Foretagsnamn {get;set;} public string BGFarg {get;set;} public string TextColor {get;set;} public string FooterFarg {get;set;} public string Email {get;set;} } [WebMethod] public static string Updatera(aData obj) { // logic code }
检查Asp.net中的jQuery Ajax JSON示例
将此格式用于ajaxpost格式:
var jSon = JSON.stringify(javascriptObject);
你的Json格式是这样的:'{name:“’+ name +’”}’,
function ShowCurrentTime() { $.ajax({ type: "POST", url: "Installningar.aspx/Updatera", data: jSon; contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, failure: function(response) { alert(response.d); } }); } function OnSuccess(response) { alert(response.d); }
按照此步骤完成代码运行: http : //www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx