2015-04-23 83 views
6

我試圖將字符串化javascript對象,然後將該字符串作爲參數傳遞給代碼隱藏中的WebMethod。我得不到它的工作,因爲我得到500的內部服務器錯誤,並且stacktrace說參數缺少值。如何將json字符串傳遞給webmethod c#ASP.NET

下面是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":"[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:

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

這種感覺就像我已經試過了,我發現一切都通過谷歌和這裏搜索時所以。

我也試過本指南中許多是指:http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

任何想法?

+0

根據'javascriptObject'的內容,您可能需要以不同的方式將其串行化。改爲在'$ .ajax'調用中設置'traditional:true'和'data:javascriptObject'。 –

+3

嘗試替換 'data:jSon,' 與 'data:{「jSon」:JSon},' – jonamreddy

+0

@jonamreddy我改變了數據:jSon改爲數據:{「jSon」:jSon},現在我仍然收到錯誤說「無效的json原語」。 –

回答

7

首先您需要使用

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 
} 

做檢查jQuery Ajax JSON Example in Asp.net

+1

我知道它的一箇舊的,但是,這將是有益的像我 我們通常使用字符串作爲paramater類型如下 '[的WebMethod] 公共靜態字符串Updatera(** **串的obj){ //邏輯代碼 }' 但是,我們必須使用參數作爲類型,在上述情況下,JSON類型是類型的** ** ADATA類 '[的WebMethod]
公共靜態字符串Updatera(** ADATA ** obj) { //邏輯代碼 } ' –

0

使用此格式的ajax post格式:

var jSon = JSON.stringify(javascriptObject); 

你的JSON格式將是這樣的: '{名: 「' +名字+ '」}',

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

相關問題