Surendra Mourya Surendra Mourya - 1 month ago 20
C# Question

Webmethod not getting hit in ajax call

I have angular js directive from where i am calling one function which has ajax post call to c# webmethod.

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


The functions which get called from directive

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);
}


It is not hitting the web method neither the success or failure getting called but control goes inside
test
function. Any clues where i am going wrong.

Answer

Add this to your ajax call. It should give you a better message to act on.

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