如何从.net中的不同命名空间调用web服务

发布于 2024-12-09 15:34:41 字数 2104 浏览 0 评论 0原文

好吧,我在这里有点麻烦。

当我尝试从属于 myproject.account.members 命名空间的页面调用 Web 服务

到属于 myproject.services 的 Web 服务时,我收到错误

///- ----修改:添加错误消息-----///

---------------------------
Message from webpage
---------------------------
responseText=
textStatus=error
errorThrown=Unknown
---------------------------
OK   
---------------------------

例如。

来自 somepage.aspx 的 jquery ajax 调用属于 myproject.account.members

$.ajax({
    type: "POST",
    url: '<%=ResolveUrl("~/Services/productService.asmx/getSomething") %>',
    data: '',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result){
        alert(result.d)
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){

alert("responseText=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);

    }
});

webservice 代码属于 myproject.Services

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace myproject.Services
{
    /// <summary>
    /// Summary description for productService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class productService : System.Web.Services.WebService
    {

        [WebMethod]
        public String getSomething()
        {
            return "Hello World";
        }
    }
}

基本上,如果我更改命名空间我的网络服务从 myproject.Servicesmyproject.account.member 如果我从属于 myproject.account.members 命名空间的页面调用它,它就可以工作......但是它如果将命名空间设置为 myproject.Services

请帮忙..我该如何解决这个问题?如果我想从项目中的几个不同命名空间调用相同的服务怎么办?`在此处输入代码

ok i'm in a bit of a pickle here.

When i try to call a webservice from a page that belongs to myproject.account.members namespace

to a webservice that belongs to myproject.services i get an error

///-----modified: added error message-----///

---------------------------
Message from webpage
---------------------------
responseText=
textStatus=error
errorThrown=Unknown
---------------------------
OK   
---------------------------

eg.

The jquery ajax call from somepage.aspx that belongs to myproject.account.members

$.ajax({
    type: "POST",
    url: '<%=ResolveUrl("~/Services/productService.asmx/getSomething") %>',
    data: '',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result){
        alert(result.d)
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){

alert("responseText=" + XMLHttpRequest.responseText + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);

    }
});

the webservice code belongs to myproject.Services

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace myproject.Services
{
    /// <summary>
    /// Summary description for productService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class productService : System.Web.Services.WebService
    {

        [WebMethod]
        public String getSomething()
        {
            return "Hello World";
        }
    }
}

Basically if i change namespace of my webservice from myproject.Services to myproject.account.member it works if i call it from a page that belongs to myproject.account.members namespace... but it won't work if set the namespace to myproject.Services

please help.. how do i get around this? what if i want to call same service from several different namespaces within my project?`enter code here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2024-12-16 15:34:41

当您更改 Web 服务类背后的代码的命名空间时,请不要忘记在标记中也更改它 (productService.asmx):

<%@ WebService 
    Language="C#" 
    CodeBehind="productService.asmx.cs" 
    Class="myproject.Services.productService" %>

同时将:替换

data: ''

为:

data: '{}'

并获得更有意义的内容错误消息比您在问题中发布的消息使用 FireBug 或 Chrome 开发人员工具来查看它发送的确切 AJAX 请求。您将看到来自服务器的请求和响应。它将帮助您更好地了解代码的问题所在。

When you changed the namespace of the code behind class of the web service don't forget to change it in the markup as well (productService.asmx):

<%@ WebService 
    Language="C#" 
    CodeBehind="productService.asmx.cs" 
    Class="myproject.Services.productService" %>

Also replace:

data: ''

with:

data: '{}'

And to get a more meaningful error message than the one you have posted in your question use FireBug or Chrome Developer tools in order to see the exact AJAX request that it being sent. You will see the request and the response from the server. It will help you better understand what is wrong with your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文