Ajax不调用Web Service

时间:2015-03-27 00:56:43

标签: javascript c# ajax web-services

我试图从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");}
    });

这是我的Web服务代码:

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

有谁能告诉我它有什么问题以及如何让它发挥作用?

1 个答案:

答案 0 :(得分:0)

尝试将方法设置为静态:

        [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();
        }