Search code examples
jqueryajaxdotnetnuke

Using a jquery ajax call with dotnetnuke


Im just trying to do the easiest ajax call such as this:

<script type="text/javascript">
$.ajax({
    type: "POST",
    url: "module.aspx/testSub",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: false,
    success: function() {
        alert("Works!");
    },
    error: function(){
        alert("Error");
    }
});
</script>

And here is my codebehind

Public Sub testSub()
    lblTest1.Text = "HelloWorld!!!1!"
End Sub

But it always returns an error.


Solution

  • A couple of things.

    First of all it will help to get a bit more information back on your error object. The Best way is to update your error function to handle the signature supported by jquery which is error(jqXHR, textStatus, errorThrown) the "errorThrown" will have some more information. (See this API Doc) Your code MIGHT look like this with it included.

    <script type="text/javascript">
    $.ajax({
        type: "POST",
        url: "module.aspx/testSub",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function() {
            alert("Works!");
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert("Error:" + errorThrown);
        }
    });
    </script>
    

    Now, aside from this, I believe you have some issues with the path. Within DNN your module resides within /DesktopModules/ModuleName/. Your Custom ASPX page that has your service methods in it resides within there. (NOTE: If you are NOT using a custom aspx page, and doing your methods inside of your ascx, you will NOT be able to call them directly.)

    Most likely your url will need to be something like /DesktopModules/MyModule/module.aspx/testusb or similar. (Also be sure to mark the method as a ScriptService using the proper attribute.)