web_ethodにhttp投稿を送信する次のangularjsコードがありますが、これ以上の情報がないと次のエラーが発生します。助けてもらえますか? webmethodにデータを送信せず、そのデータだけを取得しても、正常に動作します。
は、リソースの読み込みに失敗しました:サーバーは500(内部サーバーエラー)の状態で応答し angular.js:11442 POST http://localhost:54461/GetData.aspx/getData 500(内部サーバーエラー)
Javascriptを:
var request = "{'name':'" + "Nariman" + "'age':'" + 12 + "'}";
$scope.retData = {};
var config = {
headers: {
'Content-Type': '"application/json; charset=utf-8";',
'dataType': '"json"'
}
}
$scope.retData.getResult = function (item, event) {
$http.post('GetData.aspx/getData', request, config)
.success(function (data, status, headers, config) {
$scope.retData.result = data.d;
})
.error(function (data, status, headers, config) {
$scope.status = status;
});
}
ASPXをWebMethod属性(Cの#):
public static string getData(string name, int age)
{
string.Format("Name: {0}{2}Age: {1}", name, age, Environment.NewLine);
}
EDIT ---------------------------- ----------
私はwebmethodにjsonデータを送信しない場合、正常に動作します。例えば以下のコードが動作し、webmethodの中にブレークポイントを置くと、そこに行くことが示されます。しかし、私はJSONデータを送信する場合、それはWebMethod属性内で行っておりません。
Javaacript(任意のJSONデータを送信しない):
var config = {
headers: {
'Content-Type': '"application/json; charset=utf-8";',
'dataType': '"json"'
}
}
$scope.retData.getResult = function(item, event) {
$http.post('GetData.aspx/getData', data, config)
.success(function(data, status, headers, config) {
$scope.retData.result = data.d;
})
.error(function(data, status, headers, config) {
$scope.status = status;
});
}
ASPX(無入力のparam)
public static string getData()
{
// just having a breakpoint shows it comes inside the
// webmethod when no data is passed.
}
あなたはVAR '要求= JSONを試すことができます。 stringify({name: "Nariman"、age:12}); ' –
エラーコード' 500 'は、サーバー側のコードで例外があることを意味します。私は、あなたが例外のためにそこを見ておくべきであることを示唆します。クライアントから渡されるデータについて指摘したように、おそらくサーバー上の予期しない値のように見えます。サーバーの例外から詳細を得ることができます。 – S4beR
@EmmanuelDuraiありがとうございます。私はちょうど今試みた...まだ同じエラー。 – user3033921