【问题标题】:How to pass json string to webmethod c# ASP.NET如何将json字符串传递给webmethod c# ASP.NET
【发布时间】:2015-07-01 07:19:27
【问题描述】:

我正在尝试对 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":"<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) {

    }
});

这里是网络方法:

[WebMethod]
public static string Updatera(string jSon)
{

感觉就像我已经尝试了通过谷歌搜索时找到的所有东西。

我也试过这个很多人参考的指南:http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

有什么想法吗?

【问题讨论】:

  • 根据javascriptObject的内容,您可能需要对它进行不同的序列化。尝试在 $.ajax 调用中设置 traditional: truedata: javascriptObject
  • 尝试用data: {"jSon":JSon},替换data: jSon,
  • @jonamreddy 我已将数据:jSon 更改为数据:{“jSon”:jSon },我仍然收到错误,现在说“无效的 json 原语”。
  • 你能分享一下json的价值吗?
  • @jonamreddy 我已经用 jSon 值更新了 OP。

标签: javascript jquery asp.net ajax webmethod


【解决方案1】:

首先,你需要使用:

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){
    // Do something
}
function OnErrorCall(){
    // Do something
}

代码隐藏:

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
}

【讨论】:

  • 我知道它是一个旧的但是对像我这样的人会有用我们通常使用字符串作为参数类型,如下[WebMethod] public static string Updatera(**string** obj) { // logic code } 但是我们必须使用参数作为类类型,在上面case JSON 类型是 aData[WebMethod]&lt;br/&gt; public static string Updatera(**aData** obj) { // logic code }
【解决方案2】:

将此格式用于 ajax 帖子格式:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多