2012-03-18 18 views
1
JavaScriptSerializer jss = new JavaScriptSerializer(); 
       var seferim = jss.Serialize(sefer); 
       string asds = seferim.ToString(); 
       asds = HttpUtility.HtmlEncode(asds); 

function otobusGetir(id, b, i) { 
     id = encodeURI(id); 

     $.ajax({ 
      type: 'POST', 
      url: 'BiletSatis.aspx/Getir', 
      contentType: 'application/json;charset=utf-8', 
      dataType: 'json', 
      data: '{"id":"' + id + '","Binis":"' + b + '","Inis":"' + i + '"}', 
      success: function() { }, 
      error: function() { } 
     }) 

[的WebMethod] 公共靜態字符串Getir(串ID,串Binis,串伊尼斯) { 字符串= ID; string b = HttpUtility.HtmlDecode(id); 返回null; }通的JavaScript類對象在代碼被序列後面

問題是JavaScript函數無法接受參數(未捕獲的SyntaxError:意外的令牌非法)這是serialized.How我完全可以完成這個工作?

的問題是,這是行不通的,而JavaScript函數運行它不能採取說法,怎麼能完全以不要這份工作

+0

你可以添加JavaScript代碼嗎?你發佈的json是有效的。你應該能夠在JavaScript中反序列化。 – albertjan 2012-03-18 20:13:11

+0

我不承認你正在嘗試做什麼。你的json文字看起來是正確的。 seferim應該能夠傳遞任何js函數作爲參數,沒有問題。你能否顯示你的代碼被破壞? – 2012-03-18 20:17:07

+0

「我怎樣才能發送像上面這樣的數據是一類JavaScript」你能改說這個嗎?我認爲這是問題的主題,但我不確定。 – dwerner 2012-03-18 21:44:40

回答

0

嘗試這樣的:

function otobusGetir(id, b, i) { 
    $.ajax({ 
     type: 'POST', 
     url: 'BiletSatis.aspx/Getir', 
     contentType: 'application/json;charset=utf-8', 
     dataType: 'json', 
     data: JSON.stringify({ id: id, Binis: b, Inis: i }), 
     success: function() { }, 
     error: function() { } 
    }); 
} 

,並調用該函數:

otobusGetir(123, 'foo bar', 'baz'); 

和頁面方法:

[WebMethod] 
public static string Getir(string id, string Binis, string Inis) 
{ 
    // don't use any Html decoding here. The parameters will 
    // already be properly formatted so you can use them directly 

    return null; 
} 
+0

函數獲取參數時引起的問題,函數無法獲取它並且表示未捕獲的SyntaxError:意外的令牌ILLEGAL – Yunus 2012-03-18 22:08:43

+0

對不起我不懂你在說什麼。 – 2012-03-18 22:09:35

+0

我試圖再次解釋它 – Yunus 2012-03-18 22:25:27

相關問題