2015-02-09 58 views
0

login.aspx.csAsp.net郵政JSON

[System.Web.Services.WebMethod] 
     public static string jQueryPageMethod(string name) 
     { 
      return "<h3>jQuery - PageMethod </h3>result" + name; 
     } 

JS/jQuery的:

如果我運行下面的方法,它的工作原理。

$.ajax({ 
    type: 'POST', 
    url: 'login.aspx/jQueryPageMethod', 
    data: '{ "name":"exampleValue" }', 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: function(result) { 
     alert("success"); 
    }, 
    error: function() { 
     alert('error'); 
    } 
}); 

如果我運行下面的方法它不工作

var somevalue = "Value"; 

$.ajax({ 
    type: 'POST', 
    url: 'login.aspx/jQueryPageMethod', 
    data: '{ "name":' + somevalue + ' }', // Problem here 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: function(result) { 
    alert("success"); 
    }, 
    error: function() { 
    alert('error'); 
    } 
}); 

其中i在第二示例錯過數據的一部分?

+0

你能否更詳細地解釋什麼是你正在嘗試做? – Dana 2015-02-09 13:58:30

+0

你能提供你所得到的錯誤嗎? – 2015-02-09 13:59:18

+0

我想發佈值爲jQueryPageMethod命名方法 – Soner 2015-02-09 14:04:11

回答

0

試試這個data: '{"queryname":"' + queryname + '"}'

+0

這個作品....... :) – Soner 2015-02-09 14:08:21

+0

這很奇怪。不知何故,在發佈http請求之前,你都對數據做了些什麼。你提到了json stringify - 它將把一個字符串轉換成json對象,但這就像過河(兩次)到水中一樣... – 2015-02-09 14:20:48

+0

我不是任何東西。只是傳遞給定的文本變量。 – causita 2015-02-09 14:24:39

1

你的數據不應該被格式化爲一個字符串,而是作爲一個JavaScript對象是這樣的:

data: { "name": somevalue }, // No Problem here :) 
+0

仍然是同樣的問題。由於字符串jsonstringfiy可能會發生問題? – Soner 2015-02-09 14:03:55

+0

@Soner我認爲你不需要那個。你在哪裏使用 - 我看不出你的例子有用嗎?它看起來很直接。我的代碼應該工作,除非發佈的內容比發佈的內容多? – 2015-02-09 14:04:57