1

I am developing a dotnetnuke module and have lots of issues connecting to a webservice through javascript to retrieve data.

I have the folloiwng javascript:

function Test() {
        $.ajax({
            type: "POST",
            url: 'http://localhost/DNN11/service1.asmx/TestMethod',
            data: "{'data':'" + "abc" + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                alert("success");
            },
            error: function(e) {
                alert("error");
            }
        });


    }

Then the following WebMethod in my webservice

[WebMethod]
    public string TestMethod(String data)
    {
        return "Hello World";
    }

However when the post is not being called successfully, I am not able to get through my webservice. It keeps giving me a 500 Internal Server error page.

What could be wrong please? Is there any other way I need to go about this?

Thanks

[Edit]

This is the entire code

<script type="text/javascript">
    function Test() {

        $.ajax({
            type: "POST",
            url: '<%=ResolveUrl("~/DesktopModules/ModuleTest/WebService1.asmx/TestMethod")%>',
            data: '{ data: "test" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                alert("Success: " + msg);
            },
            error: function(msg) {
                alert("Failed: " + msg.status + ": " + msg.statusText);
            }
        });   

    }
</script>

<div>
    <input type="button" value="Load" onclick="Test(); return false;" />
</div>

[System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod()]
        public string TestMethod(string data)
        {
            return "Hello World";
        }
    }

I have tried putting the Webservice in the DotNetNuke folder in a new folder and both in the local project.. I also put the ScriptMethod() Tag in the WebService which allows scripts to call it.. but still nothing

Kelish
  • 83
  • 2
  • 10

2 Answers2

1

Looks like your problem is the cross domain calls. In case if your page hosted on the same server with the web service, your code should be fine. If you need to interact with the service from another domains you have to use jsonp requests. Look at this good step-by-step guide with the code example here.

UPDATE

In case if everything on the same app try to use relevant URL. Instead of url: '<%=ResolveUrl("~/DesktopModules/ModuleTest/WebService1.asmx/TestMethod")%>' try to url: '/DesktopModules/ModuleTest/WebService1.asmx/TestMethod'.

Mr. Pumpkin
  • 6,212
  • 6
  • 44
  • 60
  • The projects are in the same folder, on the same server.. however I still tried the post in the guide you posted and I am still having the same problems :( – Kelish Jan 27 '13 at 00:38
  • have you tried to check in Firebug, for example, where the real request go to? – Mr. Pumpkin Jan 27 '13 at 05:28
  • yes I did, it keeps showing the following: POST http://localhost/dnn11/DesktopModules/ModuleTest/WebService1.asmx/TestMethod 500 Internal Server Error – Kelish Jan 27 '13 at 09:57
  • have you tried to use relevant url instead of absolute with localhost: '/dnn11/DesktopModules/ModuleTest/WebService1.asmx/TestMethod'? – Mr. Pumpkin Jan 27 '13 at 12:36
  • yes, still the same result.. keep getting a 500 Internal Server Error... not sure if I am missing a configuration setting in the web.config of DotNetNuke – Kelish Jan 27 '13 at 12:44
  • What is written in the response when you check it in Firebug? Copy and paste it here. – Mr. Pumpkin Jan 27 '13 at 12:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23439/discussion-between-alex-wheat-and-kelish) – Mr. Pumpkin Jan 27 '13 at 13:05
  • I reinstalled DNN and created new modules.. and this time I am using an HTTP Handler instead of a Webservice (should be easier) .. but still getting the same problem.. it says the following: POST http://localhost/DesktopModules/TestModule/Handler.ashx?StrMethodName=REGISTERUSER 500 (Internal Server Error) ... and when trying to call the page directly, it returns the following: Could not load file or assembly 'DotNetNuke.HttpModules' or one of its dependencies. The system cannot find the file specified. .... I added a reference to the DLL but its still the same issue – Kelish Jan 27 '13 at 13:15
  • What is written in the response when you check it in Firebug? Copy and paste it here – Mr. Pumpkin Jan 27 '13 at 13:21
  • there should be more detailed information what is going wrong instead of just 500 (Internal Server Error)... – Mr. Pumpkin Jan 27 '13 at 13:21
0

In order to post to a DNN webservice from the page you're going to have to pass in the Context of the module. I have some sample code that works against DNN6 at

http://slidepresentation.codeplex.com/SourceControl/changeset/view/69709#1181962

Mainly the

servicesFramework: $.ServicesFramework(<%=ModuleContext.ModuleId %>)

line

I haven't tested that against DNN7 though with the latest service framework WebAPI changes.

Chris Hammond
  • 8,873
  • 1
  • 26
  • 34