Javascript 如何将json字符串传递给webmethod c#ASP.NET

Javascript 如何将json字符串传递给webmethod c#ASP.NET,javascript,jquery,asp.net,ajax,webmethod,Javascript,Jquery,Asp.net,Ajax,Webmethod,我正在尝试对javascript对象进行字符串化,然后将字符串作为参数传递给代码隐藏中的WebMethod。我无法让它工作,因为我得到一个500的内部服务器错误,stacktrace说参数缺少值 以下是javascript代码: var jSon = JSON.stringify(javascriptObject); // "{"Foretagsnamn":"Avector","BGFarg":"000000","TextColor":"fafafa","FooterFarg":"ffffff

我正在尝试对javascript对象进行字符串化,然后将字符串作为参数传递给代码隐藏中的WebMethod。我无法让它工作,因为我得到一个500的内部服务器错误,stacktrace说参数缺少值

以下是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":"<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"https://maps.google.se/maps?f=q&amp;source=embed&amp;hl=sv&amp;geocode=&amp;q=Avector AB&amp;aq=&amp;sll=56.225986,12.870827&amp;sspn=0.076248,0.154324&amp;ie=UTF8&amp;hq=Avector AB&amp;hnear=&amp;t=m&amp;cid=645910733081021950&amp;iwloc=A&amp;ll=56.224594,12.859229&amp;spn=0,0&amp;output=embed\"></iframe><br /><small><a href=\"https://maps.google.se/maps?f=q&amp;source=embed&amp;hl=sv&amp;geocode=&amp;q=Avector AB&amp;aq=&amp;sll=56.225986,12.870827&amp;sspn=0.076248,0.154324&amp;ie=UTF8&amp;hq=Avector AB&amp;hnear=&amp;t=m&amp;cid=645910733081021950&amp;iwloc=A&amp;ll=56.224594,12.859229&amp;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) {

    }
});
感觉就像我已经尝试了所有我在谷歌搜索时找到的东西

我也尝试过很多人提到的这个指南:


有什么想法吗?

将此格式用于ajax post格式:

var jSon = JSON.stringify(javascriptObject);
您的Json格式如下所示: “{name:”'+name+“}'

按照此步骤完成运行代码:
首先,您需要使用:

var jSon = JSON.stringify({obj:javascriptObject});
而不是:

var jSon = JSON.stringify(javascriptObject);
那么您的
WebMethod
将如下所示:

[WebMethod]
公共静态字符串更新(aData obj)
{
//逻辑码
}
现在这里是
aData
您的类如下所示:

公共类数据{
公共字符串Foretagsnamn{get;set;}
公共字符串BGFarg{get;set;}
公共字符串TextColor{get;set;}
公共字符串FooterFarg{get;set;}
公共字符串电子邮件{get;set;}
}

最后的代码看起来像 jQuery:

var jSon=jSon.stringify({obj:javascriptObject});
$.ajax({
类型:“POST”,
url:“Post/Installningar.aspx/Updatera”,
资料来源:jsonData,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:一旦成功,
错误:OnErrorCall
});
函数OnSuccess(响应){
//做点什么
}
函数OnErrorCall(){
//做点什么
}
代码隐藏:

公共类数据{
公共字符串Foretagsnamn{get;set;}
公共字符串BGFarg{get;set;}
公共字符串TextColor{get;set;}
公共字符串FooterFarg{get;set;}
公共字符串电子邮件{get;set;}
}
[网络方法]
公共静态字符串更新(aData obj)
{
//逻辑码
}

根据
javascriptObject
的内容,您可能需要对其进行不同的序列化。尝试在
$.ajax
调用中设置
traditional:true
data:javascriptObject
。尝试将
data:jSon,
替换为
data:{“jSon”:jSon},
@jonamreddy将data:jSon更改为data:{“jSon”:jSon},我仍然会出错,现在说“无效的jSon原语”。你能分享jSon值吗?@jonamreddy我已经用jSon值更新了OP。我知道这是一个旧的,但对像我这样的人来说会很有用。我们通常使用字符串作为参数类型,如下
[WebMethod]公共静态字符串更新(**string**obj){//logic code}
,但我们必须使用参数作为类类型,在上述情况下,JSON类型是aData类
[WebMethod]
公共静态字符串更新(**aData**obj){//logic code}
var jSon = JSON.stringify({obj:javascriptObject});
var jSon = JSON.stringify(javascriptObject);