加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 带有angularjs的$http请求正在返回我的html doc

发布时间:2020-12-15 22:05:48 所属栏目:百科 来源:网络整理
导读:我是新来的,很抱歉,如果我在帖子上犯了一些错误…… 我正在尝试将angularjs与ASP.NET Web Forms一起使用,一切顺利,直到我需要向c#webmethods发出请求.我在这里和其他页面上搜索解决方案并没有找到任何东西. 让我们来解决问题:我的请求只返回我的Default.asp
我是新来的,很抱歉,如果我在帖子上犯了一些错误……

我正在尝试将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;
        });

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读