2016-01-25 57 views
2

我想通過Angular $ http發送一個字符串到我的ASP.NET C#後端。我正在使用網絡表單。不過,我得到以下錯誤:

POST http://localhost:49730/Default.aspx/get_requested_table 404 (Not Found) 

我的角度控制器看起來是這樣的:

App.controller("MainCtrl", function ($scope, $http) { 
    $scope.request_table = function (tableName) { 
     $http 
      .post("Default.aspx/get_requested_table", tableName) 
      .success(function (data) { 
       console.log("Success, it worked!"); 
      }); 
    }; 
}); 

我的HTML看起來像這樣:

<button ng-click="request_table('tblMain')" type='button'>GET IT</button> 

我的ASP.NET C#文件(aka Default.aspx.cs):

public static string myTable; 
[WebMethod] 
public static string get_requested_table(string tableName) 
{ 
    var myTable = tableName; 
    Console.Write(myTable); 
    return myTable; 
} 

我做錯了什麼,得到這個錯誤?我如何使用Angular的$ http方法與我的C#後端進行通話?

+0

你有一個ScriptManager您的網頁上? (https://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager(v=vs.110).aspx) –

回答

1

您還沒有發送鍵/值對

嘗試

$http.post("Default.aspx/get_requested_table", {tableName: tableName}) 
+0

對不起,我意識到我可能一直混淆通過調用變量表。它應該真的是tableName,因爲它是一個字符串。那麼我是否必須將鍵/值對更改爲{string:tableName}? –

+1

鍵是對象鍵...讓它匹配後端 – charlietfl

+0

太棒了!這工作!我設置了{tableName:tableName},謝謝! :) –

相關問題