0

I'm trying to execute the follow code below, from Run db query (sql server 2005) with ajax. Is it possible?.

<Head>
    <script type="text/javascript">
    function runAjax() {
        $.ajax({
            type: "POST",
            url: "Index.aspx/PerformDatabaseQuery",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                alert("Test");
            }
    });
}
</script>
</Head>

I then have a button in the body which calls the function

<button type="button" onclick="runAjax();"/>

The code above resides in Home.cshtml

I'm not entirely sure if I created Index.aspx.cs correct, I clicked on a folder, add new class and simply named the file Index.aspx.cs. I put breakpoints in Index.aspx.cs and it's never hit. The callback function alert("Test") never triggers as well.

If I replace runAjax(); to execute

<script type="text/javascript">   
function functionTwo() { alert('You clicked the bottom text'); }
</script>

The alert will display. May I ask where am I going wrong with this ajax call

8
  • Are there any errors in the browser console? Commented Nov 27, 2015 at 16:38
  • Hi @Izzy I'm fairly new to web development, may I ask how would I check? Commented Nov 27, 2015 at 16:42
  • Press the F12 key. Depending on the browser you'll either have a window popup or docked and in the there you'll have a tab which will say Console click on it and refresh your page Commented Nov 27, 2015 at 16:43
  • Is it ASP.NET MVC? Or ASP.NET? If the former, there is no concept of web methods, if the latter, there should be no cshtml source files afaik. Also you button may trigger a page reload Commented Nov 27, 2015 at 16:44
  • @stuartd, then how could Home.cshtml be? Is it some latest update that allows one to use razor in asp.net? Commented Nov 27, 2015 at 16:47

1 Answer 1

0

I'd recommend that you handle the error function in the ajax call, just to see if there is anything useful being returned by the ajax call (error: function(xhr, message, exception){ //do something}. A couple of things I note as well: If you are not passing data then don't include the data option, if this is not on a dev machine on local host in the root then the call will fail as the address 'Index.aspx...' needs to be fully qualified.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.