1

I am working on asp.net web forms application. I am trying to make an ajax call to a web method in codebehind, but instead of returning the result, it returns the whole html page.

I am calling it on button click

 <input type="button" id="btnCallAPIFromClient" class="btn btn-success" value="Call API from Client"/>

My js script is:

$(document).ready(function() {
    //PageMethods.set_path(PageMethods.get_path() + '.aspx');
    $('#btnCallAPIFromClient').click(function() {
        alert('here');
        $.ajax({
            url: '/login/GetAccessToken',
            type: "POST",
            dataType: 'html',
            success: function(response) {
                alert(response);
                debugger;
                sessionStorage.setItem("accessToken", response.access_token);
                alert(response.access_token);
            },
            // Display errors if any in the Bootstrap alert <div>
            error: function(jqXHR) {
                alert(jqXHR.responseText);
            }
        });
    });
});

webmethod is:

   [WebMethod]
        public static string GetAccessToken()
        {
           return "abc";
        }
Prashant Pokhriyal
  • 3,727
  • 4
  • 28
  • 40
Asif Hameed
  • 1,353
  • 9
  • 25
  • 46

1 Answers1

0

This is because you have set the datatype as html. Change the datatype to text as follows:

dataType: 'text'

And also in the success function change this line

sessionStorage.setItem("accessToken", response.access_token);

To

sessionStorage.setItem("accessToken", response);

Because you are not receiving data in json format. So this will be invalid.

Prashant Pokhriyal
  • 3,727
  • 4
  • 28
  • 40