C# 带有angularjs的$http请求正在返回我的html文档

C# 带有angularjs的$http请求正在返回我的html文档,c#,javascript,asp.net,angularjs,C#,Javascript,Asp.net,Angularjs,我是新来的,所以很抱歉如果我在帖子上犯了一些错误 我正在尝试将angularjs与ASP.NET Web表单一起使用,在我需要向c#webmethods发出请求之前,一切都进展顺利。我在这里和其他页面上搜索解决方案,但什么也没找到 让我们转到te问题:我的请求只返回Default.aspx html代码,而不是JSON。事实上,我的请求没有调用我的webmethod app.controller('CtrlBoletos',function($scope, FactoryBoleto) {

我是新来的,所以很抱歉如果我在帖子上犯了一些错误

我正在尝试将angularjs与ASP.NET Web表单一起使用,在我需要向c#webmethods发出请求之前,一切都进展顺利。我在这里和其他页面上搜索解决方案,但什么也没找到

让我们转到te问题:我的请求只返回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;

}
[WebMethod]
公共静态字符串get_boleto()
{     
List lista=新列表();
JavaScriptSerializer serializer=新的JavaScriptSerializer();
TFechamento fechamento=新TFechamento(2);
lista=fechamento.getDados(1.Boleto);
var json=serializer.Serialize(lista);
返回json;
}
啊,如果我用JQuery AJAX发出请求,我会得到json


有人帮帮我,我快疯了。。。对不起,我的英语不好:你缺少[ScriptMethod(UseHttpGet=true)]吗

[WebMethod]
[脚本方法(UseHttpGet=true)]
公共列表GetAllProjectName()
{
返回GetProjectName();
}
使用jqueryajax/

检查其他一些线程:


我和你有同样的问题。到目前为止,我已经创建了一个替代解决方案,以从我的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更好的解决方案,请告诉我

出于好奇/学习,我修补了这个:

问题是
content-type
标题-您将获得
aspx
页面(jQuery和Angular..any都有)

ASP.NET AJAX内容类型标头验证

ASP.NET对基于GET和POST的ASP.NET AJAX web方法都实施了一个内置的验证层保护,即无论使用何种HTTP谓词,ASP.NET始终要求将HTTP内容类型头设置为值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呼叫:

角度XHR没有数据,
内容类型
未设置:

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
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
角度XHR使用数据,
内容类型
设置为:

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
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
新的角度,所以如果有更好/更简单的方法,我会推迟


Hth…

我通过设置标题、响应类型和添加空数据对象解决了这个问题

factory.handshake = function () {
    return $http({
        method: "POST",
        url: urlBase.concat("/Handshake"),
        data: {},
        headers: { "Content-Type": "application/json" },
        responseType: 'json'
    });
};
…当我调用我的webmethod时,结果神奇地出现了

    myFactory.handshake()
        .success(function (fromApi) {
            alert(fromApi.d);
        })
        .error(function (error) {
            $scope.status = 'Unable to load sport data: ' + error.message;
        });

旁注:这里有一个葡萄牙语版本的stack:您可以在浏览器(Chrome、IE或Firefox)中使用开发者工具(F12)检查这两个请求,看看您缺少什么,正如您所说的,可以使用jQuery进行请求。我这样做了,请求是相同的,我使用firebug查看它们。。。已经更改了$http请求的头,使它们相等并且不起作用@SergioGarciaDidn不工作,请求的结果是html页面尚未…:(