【问题标题】:Web Service is not called by AjaxAjax 不调用 Web 服务
【发布时间】:2015-03-27 00:56:42
【问题描述】:

我正在尝试从 ajax 调用 Web 服务并获取字符串数组作为回报,但无论我做什么,我在控制台日志中都出现 404 错误。

这是我的客户端代码:

 $.ajax({
        url: "http://localhost:55397/WebService1.asmx/GetList",
        cache: false, type: "POST", contentType: "application/json; charset=utf-8",

        dataType: 'json',
        success: function (data) {
            tags = data.d;
        },
        error: function () { alert("AutoComplete Server not found");}
    });

这是我的网络服务代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ExpressDeal.Models;
using ExpressDeal.Controllers;
using System.Web.Script.Services;


namespace ExpressDeal
{
    /// <summary>
    /// Summary description for WebService1
    /// </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. 

    public class WebService1 : System.Web.Services.WebService
    {
        private BaseLogic bl = new BaseLogic();

        [WebMethod]
        [ System.Web.Script.Services.ScriptMethodAttribute()]
        //[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Xml)]
        public string [] GetList()
        {
            return bl.Context.Items.Select(i=>i.Name).ToArray();
        }
    }
}

谁能告诉我它有什么问题以及如何使它工作?

【问题讨论】:

  • 从简单开始。将 URL http://localhost:55397/WebService1.asmx/GetList 粘贴到浏览器栏中会发生什么情况?
  • AFAIK,asmx 不支持 JSON 序列化。它总是 SOAP。似乎您想使用 WebAPI 而不是 asmx。
  • 错误:“/”应用程序中的服务器错误。
  • 当然你不能这样调用你的网络服务。 Webservice 正在使用 SOAP(默认),您尝试通过 POST 方法调用它?

标签: javascript c# ajax web-services


【解决方案1】:

尝试将您的方法设置为静态:

        [WebMethod]
        [ System.Web.Script.Services.ScriptMethodAttribute()]
        //[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Xml)]
        public static string [] GetList()
        {
            return bl.Context.Items.Select(i=>i.Name).ToArray();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多