2012-03-29 84 views
1

我目前正在嘗試在javascript的aspx.cs裏調用一個方法。從javascript調用aspx.cs方法

下面是我目前有:

的Javascript:

function loadPins(passValue) { 
    callServer2("myMethod", passValue); 
} 

function callServer2(requestMethod, clientRequest) { 
var pageMethod = "Default.aspx/" + requestMethod; 

$.ajax({ 
    type: 'POST', 
    data: clientRequest, 
    dataType: 'JSON', 
    contentType: 'application/json', 
    url: pageMethod , //Method to call 
    success: function (result, status) { 
     alert("success"); 
    }, 
    error: function (xhr, status, error) { 
     alert("ERROR"); 
    } 
    }); 
} 

aspx.cs

[WebMethod] 
public static string myMethod(string passedVal) 
{ 
    value = passedVal; 
    return "true"; 
} 

當我調試我看到它調用並輸入CallServer2,但它似乎永遠不會打我在aspx中的斷點。我沒有看到我的成功或錯誤消息警報。

有什麼建議嗎?

錯誤我目前入門:

error - <html> 
     <head> 
     <title> Unknow we method myMethod.<br>Parameter name: 
    methodName</title> 
    <style> 
     body{font-family:verdana";font-weight:normal;font-size: 
     .7em;color:black} 
      p 
    {font-family:"Verdana";font-weightbold;color:black;margin-top: -5px} 
b{font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} 
H1{ 
font-family:"veranda";font-weight:normal;font-size:18pt;colour:red} 
H2{ 
font-family:"Verdana";font-weight:normal ;font-size:14pt color:maroon} 
pre{font-family:"Lucida Console";font-size .9em} 
.marker{font-weight:bold;color:black;text-decoration:none;} 
.version{color:gray;} 
.error{margin-bottom:10px} 
.expandable{text-decoration:underline, font-weight:bold; color:navy; cursor:hand;} 
</style> 
<head> 

<code> 
An unhanded exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code> 
... 

* 注意我沒有把......這就是真正意義上面說上面後

+0

可以通過服務器功能進行調試,如果不再檢查代碼,您的路徑必須是錯誤的。 – 2012-03-29 05:19:23

+0

您還可以顯示更新$ .ajax函數。 – 2012-03-29 05:22:46

回答

0
$.ajax({ type: "POST", 
     url: pageMethod, contentType: "application/json; charset=utf-8", 
     data: "{passedVal:" + JSON.stringify(clientRequest) + "}", dataType: "json", 
     success: function (result, status) { 
     alert("success"); 
    }, 
    error: function (xhr, status, error) { 
     alert("ERROR"); 
    } 

    }); 

你應該在數據與passedVal傳遞數據,也字符串化的變量。希望這有助於..

+0

是否還有其他我需要做的事情?我得到了一個非常長的錯誤,看起來像html代碼 – user1219627 2012-03-29 04:45:59

+0

你正在得到什麼錯誤?你也可以檢查螢火蟲中的錯誤.. – 2012-03-29 04:48:39

+0

即時通訊使用視覺工作室與IE瀏覽器,所以我能夠通過該計劃。我會在我的問題發佈錯誤,因爲它的這麼長時間,如果多數民衆贊成在你身上 – user1219627 2012-03-29 04:54:20

0

只要確保你有這樣的在你的ClientRequest

data: "{'passedVal':'" + your_data+ "'}", 

希望這有助於你!