【问题标题】:how to call webservice from a different namespace in .net如何从.net中的不同命名空间调用webservice
【发布时间】:2011-12-05 20:46:27
【问题描述】:

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

当我尝试从属于 myproject.account.members 命名空间

的页面调用网络服务时

到属于 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";
        }
    }
}

基本上,如果我将 web 服务的命名空间从 myproject.Services 更改为 myproject.account.member,如果我从属于 myproject.account 的页面调用它,它就会起作用.members 命名空间...但如果将命名空间设置为 myproject.Services 将不起作用

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

【问题讨论】:

  • “我得到一个错误”没有给我们足够的细节。请告诉我们实际发生的情况。

标签: jquery .net ajax web-services namespaces


【解决方案1】:

当您更改 Web 服务类的代码名称空间时,不要忘记在标记中也进行更改 (productService.asmx):

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

同时替换:

data: ''

与:

data: '{}'

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

【讨论】:

  • 是的,我遇到了一个不同的 .net 错误...我也确实在标记中更改了它...在 responsetext 是 .net 错误页面之前...现在 responsetext 为空白,并且 textstatus =错误
  • @robert,您在 FireBug 中看到的服务器发送的确切响应是什么?通常它会包含 YSOD 和错误信息。或者也许你得到一个404?只有开发人员工具栏可以帮助您回答这些问题并确定失败的确切原因。
  • 刚刚意识到...更改标记时,我放了 myproject.Services 而不是 myproject.Services.productService .... doh .. . 道歉........问题解决了!!!...... json data:'' 部分是小事,我只是拿出了真实的数据来制作它更简单......我没有问题
  • 为了在 5 分钟内解决这个问题,我已经解决了好几个小时......哈哈...... ty Darin 和 stackoverflow Rocks
猜你喜欢
  • 1970-01-01
  • 2011-10-28
  • 2011-11-15
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多