[英]$http request with angularjs is returning my html doc
我是新来的,很抱歉,如果我在帖子上犯了一些错误......
我正在尝试将angularjs与ASP.NET Web Forms一起使用,一切顺利,直到我需要向c#webmethods发出请求。 我在这里和其他页面上搜索解决方案并没有找到任何东西。
让我们来解决问题:我的请求只返回我的Default.aspx html代码,而不是我的JSON。 事实上,我的请求不会调用我的webmethod ...
app.controller('CtrlBoletos',function($scope, FactoryBoleto) {
$scope.selections = [];
$scope.boletos = FactoryBoleto.getBoletos();
$scope.gridBoletos = {
data: 'boletos',
selectedItems: $scope.selections,
multiSelect: false
};
});
app.factory('FactoryBoleto', function ($http) {
var getBoletos = function() {
$http({
method: 'POST',
url: "./Default.aspx/get_boleto",
async: false
}).success(function(result){
console.info(result);
});
};
return { getBoletos: getBoletos };
});
这是我的webmethod
[WebMethod]
public static string get_boleto()
{
List<TFechamento_Boleto> lista = new List<TFechamento_Boleto>();
JavaScriptSerializer serializer =new JavaScriptSerializer();
TFechamento fechamento = new TFechamento(2);
lista = fechamento.getDados(1).Boleto;
var json = serializer.Serialize(lista);
return json;
}
啊,如果我用JQuery AJAX发出请求,我会得到json ...
有人帮助我,我对此感到疯狂......对不起我的坏英语:p
我通过设置头文件,responseType和添加一个空数据对象来解决这个问题
factory.handshake = function () {
return $http({
method: "POST",
url: urlBase.concat("/Handshake"),
data: {},
headers: { "Content-Type": "application/json" },
responseType: 'json'
});
};
......而且,当我打电话给我的网络方法时,结果会神奇地出现
myFactory.handshake()
.success(function (fromApi) {
alert(fromApi.d);
})
.error(function (error) {
$scope.status = 'Unable to load sport data: ' + error.message;
});
你错过了[ScriptMethod(UseHttpGet = true)]吗?
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public List<ProjectOfEmployee> GetAllProjectName()
{
return GetProjectName();
}
检查,其他一些线程:
https://groups.google.com/forum/#!msg/angular/AQ_caU-qVJ0/Csugs6zI2-EJ
我和你有同样的问题。现在我已经创建了一个替代解决方案来从我的c#webmethod获取json结果:
$scope.yourscopefunction=function(){
$.ajax({
type: "POST",
url: "webmethodURL",
contentType: "application/json;charset=utf-8",
success: function (data) {
$.map(data.d, function (dataobject) {
alert(dataobject.yourobjectfield);
});
},
error: function (xhr, status, errorThrown) {
alert(status + "* " + errorThrown + " * " + xhr);
}
});
};
如果您使用$ http有更好的解决方案,请告诉我
出于好奇/学习Angular,我对此进行了修改:
问题是content-type
标题 - 如果你没有定义它,不调用WebMethod你会获得aspx
页面(在jQuery和Angular中实际上都是)
ASP.NET AJAX内容类型标头验证
ASP.NET强制执行基于GET和POST的ASP.NET AJAX Web方法的内置验证保护层,无论使用何种HTTP动词, ASP.NET始终要求HTTP Content-Type header设置为值application / json 。 如果未发送此内容类型标头,ASP.NET AJAX将拒绝服务器上的请求。
所以尝试这个,而不是真正发送任何数据,但Angular将设置标题:
$http.post("default.aspx/get_boleto", {});
或者在你的情况下:
$http({
method: 'POST',
url: "./Default.aspx/get_boleto",
data: {}, //nothing really, just doing this so the header is set
async: false
}).success(.....
如果您检查XHR呼叫:
没有数据的Angular XHR, 未设置Content-Type
:
POST /default.aspx/helloWorld HTTP/1.1
Host: localhost:51120
Connection: keep-alive
Content-Length: 0
Cache-Control: max-age=0
Accept: application/json, text/plain, */*
Origin: http://localhost:51120
User-Agent: ....
Referer: ....
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
数据 ,角度XHR Content-Type
设置 :
POST /default.aspx/helloWorld HTTP/1.1
Host: localhost:51120
Connection: keep-alive
Content-Length: 2
Cache-Control: max-age=0
Accept: application/json, text/plain, */*
Origin: http://localhost:51120
User-Agent:...
Content-Type: application/json;charset=UTF-8
Referer: ....
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Angular的新功能,如果有更好/更简单的方法,我会推迟......
心连心...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.