C# 无法在.net c中使用ajax调用Web服务器以获取返回结果#
我曾尝试使用以下代码从Web服务器(test.asmx.cs)检索数据,但不知何故,这总是会将错误抛给我。。。有人知道出了什么问题吗C# 无法在.net c中使用ajax调用Web服务器以获取返回结果#,c#,jquery,.net,ajax,C#,Jquery,.net,Ajax,我曾尝试使用以下代码从Web服务器(test.asmx.cs)检索数据,但不知何故,这总是会将错误抛给我。。。有人知道出了什么问题吗 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace Test { /// <summary> /// S
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Test
{
/// <summary>
/// Summary description for autocomplete
/// </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 autocomplete : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public static string streetNameSearch(int id)
{
return "Melbourne|North Melbourne|South Melbourne|Richmond|North Richmond";
}
}
}
试试这个
public static JsonResult streetNameSearch(string id)
{
return Json("Melbourne|North Melbourne|South Melbourne|Richmond|North Richmond");
}
在您的javascript中,更改如下
data: {id:"1"}
取消对此行的注释:
// [System.Web.Script.Services.ScriptService]
ScriptService属性允许ASMX服务的所有方法使用原始JSON进行响应。除了取消对
ScriptService
行的注释外,为什么当您发布的代码来自ASMX服务时,$.ajax()
方法以ASPX路径为目标?这肯定会导致问题。抛出的错误是什么?@jon3laze:错误只是“错误”,在方法streetNameSearch中放置一个断点,并检查是否调用了该方法?@jayantha,这已进入错误部分,甚至无法调用该方法streetNameSearch@Jayanthan:看起来像JsonResult和Json()在.net c#webservice中不支持
// [System.Web.Script.Services.ScriptService]