最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - ASP .NET call Webmethod with ajax request from javascript - Stack Overflow

programmeradmin0浏览0评论

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 / as its default namespace.

Remendation: 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. / 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 pany'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 "/":

C#

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/ as its default namespace.

Remendation: 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/ 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 pany'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./webservices/":

C#

Share Improve this question edited May 12, 2015 at 20:55 luca.p.alexandru asked May 12, 2015 at 18:28 luca.p.alexandruluca.p.alexandru 1,7606 gold badges24 silver badges46 bronze badges 1
  • Set the Content-Type to the appropriate MIME. See here: stackoverflow./questions/17185814/… – Howard Renollet Commented May 12, 2015 at 18:55
Add a ment  | 

3 Answers 3

Reset to default 1

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");
             })
         });

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?

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
            },
发布评论

评论列表(0)

  1. 暂无评论