问题描述
我试图对javascript对象进行字符串化,然后将该字符串作为参数传递给Code Behind中的WebMethod。我无法让它工作,因为我得到500的内部服务器错误,堆栈跟踪说参数缺少值。
Im trying to stringify a javascript object and then pass the string as a parameter to a WebMethod in Code Behind. I can't get it to work as I get a Internal Server Error of 500 and the stacktrace says that value is missing for parameter.
这是javascript代码:
Here is the 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":"[email protected]","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:
And here Is the webmethod:
[WebMethod]
public static string Updatera(string jSon)
{
感觉就像我在谷歌和SO搜索时发现的所有内容都已经尝试过了。
It feels like I've tried everything that I've found when searching by google and here on SO.
I我们也尝试过很多人提到的指南:
I've also tried this guide that many refer to: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
任何想法?
推荐答案
首先你需要使用
var jSon = JSON.stringify({obj:javascriptObject});
而不是
var jSon = JSON.stringify(javascriptObject);
然后你的webmethod就像
Then you webmethod would be like
[WebMethod]
public static string Updatera(aData obj)
{
// logic code
}
现在这里aData是你的类,如下所示
Now here aData is your class something like 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 your final code look like
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
}
请检查
Do check jQuery Ajax JSON Example in Asp.net
这篇关于如何将json字符串传递给webmethod c#ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!