javascript - How to pass json string to webmethod c# ASP.NET -
im trying stringify javascript object , pass string parameter webmethod in code behind. can't work internal server error of 500 , stacktrace says value missing parameter.
here javascript code:
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":"<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"https://maps.google.se/maps?f=q&source=embed&hl=sv&geocode=&q=avector ab&aq=&sll=56.225986,12.870827&sspn=0.076248,0.154324&ie=utf8&hq=avector ab&hnear=&t=m&cid=645910733081021950&iwloc=a&ll=56.224594,12.859229&spn=0,0&output=embed\"></iframe><br /><small><a href=\"https://maps.google.se/maps?f=q&source=embed&hl=sv&geocode=&q=avector ab&aq=&sll=56.225986,12.870827&sspn=0.076248,0.154324&ie=utf8&hq=avector ab&hnear=&t=m&cid=645910733081021950&iwloc=a&ll=56.224594,12.859229&spn=0,0\" style=\"text-align:left\">visa större karta</a></small>","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) { } });
and here webmethod:
[webmethod] public static string updatera(string json) {
it feels i've tried i've found when searching google , here on so.
i've tried guide many refer to: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
any ideas?
first need use
var json = json.stringify({obj:javascriptobject});
instead of
var json = json.stringify(javascriptobject);
then webmethod
[webmethod] public static string updatera(adata obj) { // logic code }
now here adata class below
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;} }
so final code
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(){ }
code behind :
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 }
do check jquery ajax json example in asp.net