【发布时间】: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