luca.p.alexandru
luca.p.alexandru

Reputation: 1750

ASP .NET call Webmethod with ajax request from javascript

I am calling this from an iFrame! I think this would be the main problem.

I am trying to call a WebMethod in ASP with an ajax request. But I am not getting the desired respone. This is the code for my page. And I call it like

Ext.Ajax.request({url:  'Default.aspx/DispatchMethod'});

The problem is I get html in the response instead of the string "TEST". Any idea why?

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static String DispatchMethod() 
    {
        return "TEST";
    }
}

After using a WebService (.asmx extension) I just get a

This web service is using http://tempuri.org/ as its default namespace.

Recommendation: Change the default namespace before the XML Web service is made public.

Each XML Web service needs a unique namespace in order for client applications to distinguish it from other services on the Web. http://tempuri.org/ is available for XML Web services that are under development, but published XML Web services should use a more permanent namespace.

Your XML Web service should be identified by a namespace that you control. For example, you can use your company's Internet domain name as part of the namespace. Although many XML Web service namespaces look like URLs, they need not point to actual resources on the Web. (XML Web service namespaces are URIs.)

For XML Web services creating using ASP.NET, the default namespace can be changed using the WebService attribute's Namespace property. The WebService attribute is an attribute applied to the class that contains the XML Web service methods. Below is a code example that sets the namespace to "http://microsoft.com/webservices/":

C#

Upvotes: 2

Views: 9419

Answers (3)

moarboilerplate
moarboilerplate

Reputation: 1643

Okay, let's clear something up. You're using a page method, not an XML Web Service, and you really really don't want to go down the dark path of using an asmx service just to expose an endpoint for client script.

Your page method syntax looks all right. Your issue is probably with the options you've provided (most likely because the content-type is not being set as application/json).

You mentioned you get HTML back. Well, what kind of HTML? Could it perhaps be an error page?

Upvotes: 1

Tez Wingfield
Tez Wingfield

Reputation: 2251

I'm not sure if your code structure was intended for the purpose of the Q, If not, then you may need start by decoupling and focus on a key element of "ContentType" in your AJAX request.

Start by adding a .asmx (which is what [WebMethod] refers too) file to your solution.

Then add the method:

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello, World";
    }

Now assuming your using an external js sheet, apply the following logic (Onload or OnClick):

        $.ajax
         ({
             type: "POST",
             contentType: "application/json; charset=utf-8",
             url: "/webservice-folder/webservice.asmx/HelloWorld",
             success: (function (data) {
                 alert(data.d)
             }),
             error: (function () {
                 alert("error");
             })
         });

Upvotes: 1

Millar248
Millar248

Reputation: 401

I think you need the url to be: "DispatchMethod"

Could be wrong.

You also might need more fields, like this:

$.ajax({
            dataType: "json",
            url: "DispatchMethod",
            data: {
                "customerNo": customerNo,
                "productNo": productNo
            },

Upvotes: 0

Related Questions