Angularjs $http.post - 将参数作为 JSON 发送到 ASPX webmethod
Angularjs $http.post - sending params as JSON to ASPX webmethod
我有以下 angularjs 代码将 http post 发送到网络方法,但我收到以下错误,没有更多信息。有人可以帮忙吗?
如果我不向 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 网络方法 (C#):
public static string getData(string name, int age)
{
string.Format("Name: {0}{2}Age: {1}", name, age, Environment.NewLine);
}
编辑 ---------------------------------- ----
如果我不向网络方法发送任何 json 数据,它就可以正常工作。例如,下面的代码有效,如果我在 web 方法中放置断点,它会显示它在那里。但是如果我发送 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(没有输入参数时)
public static string getData()
{
// just having a breakpoint shows it comes inside the
// webmethod when no data is passed.
}
您的问题似乎与 Emmanual Durai 在您问题的第一条评论中指出的一样:var request = "{'name':'" + "Nariman" + "'age':'" + 12 + "'}";
不是有效的 json 对象。
request 会给你 {'name':'Nariman'age':'12'}
作为字符串,它不会解析为 JSON(格式有问题)。
您应该尝试类似下面的方法来获得有效的字符串
var request = {
name: "Nariman",
age: 12
}
var requestString = JSON.stringify(request)
也请看这里How to pass json POST data to Web API method as object。您的问题通常不是 angularjs $http
特有的,但通常是 XHR
请求。
只需更改:
var request = "{'name':'" + "Nariman" + "'age':'" + 12 + "'}";
收件人:
var request = { name: "Nariman", age: 12 };
您不必使用 JSON.stringify()
var request = { name: "Nariman", age: 12 };
var config = {
headers: {
"Content-Type": "application/json; charset=utf-8"
}
}
$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;
});
我有以下 angularjs 代码将 http post 发送到网络方法,但我收到以下错误,没有更多信息。有人可以帮忙吗? 如果我不向 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 网络方法 (C#):
public static string getData(string name, int age)
{
string.Format("Name: {0}{2}Age: {1}", name, age, Environment.NewLine);
}
编辑 ---------------------------------- ----
如果我不向网络方法发送任何 json 数据,它就可以正常工作。例如,下面的代码有效,如果我在 web 方法中放置断点,它会显示它在那里。但是如果我发送 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(没有输入参数时)
public static string getData()
{
// just having a breakpoint shows it comes inside the
// webmethod when no data is passed.
}
您的问题似乎与 Emmanual Durai 在您问题的第一条评论中指出的一样:var request = "{'name':'" + "Nariman" + "'age':'" + 12 + "'}";
不是有效的 json 对象。
request 会给你 {'name':'Nariman'age':'12'}
作为字符串,它不会解析为 JSON(格式有问题)。
您应该尝试类似下面的方法来获得有效的字符串
var request = {
name: "Nariman",
age: 12
}
var requestString = JSON.stringify(request)
也请看这里How to pass json POST data to Web API method as object。您的问题通常不是 angularjs $http
特有的,但通常是 XHR
请求。
只需更改:
var request = "{'name':'" + "Nariman" + "'age':'" + 12 + "'}";
收件人:
var request = { name: "Nariman", age: 12 };
您不必使用 JSON.stringify()
var request = { name: "Nariman", age: 12 };
var config = {
headers: {
"Content-Type": "application/json; charset=utf-8"
}
}
$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;
});