0

I have coded an AJAX GET request using a XMLHttpRequest object. My problem is that the request is never hitting the server side code. I had a look at many forums and at stackoverflow and I dont really know what I am missing.

My JS code

    function loadXMLDoc() {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var response = xmlhttp.responseText;
            }
        }

        xmlhttp.open("GET", "login.aspx/GetData", true);
        xmlhttp.setRequestHeader("Content-Type", "application/json");
        xmlhttp.send(null);
    }

Server side code - login.aspx

    [WebMethod]
    public static string GetData()
    {
        return "break here";
    }

Looking at the Network tab in developer tools, I see that it cannot find the method as the error code is 505 (Internal Server Error)

Can this work within an aspx page? maybe only wrks in a asmx service?

Is there anything in the code that I have not done or is wrong?

Thanks

user3428422
  • 4,300
  • 12
  • 55
  • 119
  • Why have you got `xmlhttp.setRequestHeader("Content-Type", "application/json");` in there? You are making a GET request. There is no content to describe the type of. – Quentin Jan 26 '15 at 11:55
  • 1
    Look at your browser's developer tools. Look at the JavaScript console. Does it report any errors? Look at the Net tab. Is the request being made? Does it get a response? Do they contain the data you expect? – Quentin Jan 26 '15 at 11:55
  • It was stated in another article. Taking it out still causes the same problem – user3428422 Jan 26 '15 at 11:56
  • Look at your browser's developer tools. - not a bad idea. Will do. – user3428422 Jan 26 '15 at 11:57
  • if you receive a 500 in the response, please referrer too http://stackoverflow.com/questions/10750631/calling-a-webmethod-using-jqueryajax-get – user3428422 Jan 26 '15 at 13:21

0 Answers0