Webmethod 未在 ajax 调用中被击中

Webmethod not getting hit in ajax call

我有 angular js 指令,我从那里调用一个函数 ajax post 调用 c# webmethod。

app.directive("fileread", [function () {
return {
link: function ($scope, $elm, $attrs) {
  $elm.on('change', function (changeEvent) {
   var data = "some json data";   
        test(data);           
      });
    };
  };
}]);

指令调用的函数

function test(json_object){
     $.ajax({
        type: "POST",
        url: "/sites/Demo/_layouts/15/demo/demowebmethod.aspx/mywebmethod",
        data: json_object,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
           alert(response.d);
        }
     });    
}

function OnSuccess(response) {
    alert(response.d);
}

调用成功或失败都不会触发 Web 方法,但控制会进入 test 函数。我出错的任何线索。

将此添加到您的 ajax 通话中。它应该给你一个更好的信息来采取行动。

 error: function (request, status, error) {
                alert('Error: ' + error);
            }

$.ajax:

中没有 failure 回调
failure: function(response) {
     alert(response.d);
}

将其更改为 error: 或使用 promise

error: function(response) {
     alert(response.d);
}

http://api.jquery.com/jquery.ajax/

可能是网络方法导致的错误,但您没有看到。通过将 url 更改为明显不存在的内容来确认,例如“.../mywebmethodxxxxxxx”