Asmx通过ajax响应json和调用

Asmx通过ajax响应json和调用,json,ajax,xml,jsonp,asmx,Json,Ajax,Xml,Jsonp,Asmx,我有以下问题 我正在尝试使用jquery将HTML页面与asmx服务连接起来。 该服务位于我的本地IIS中,我已将其配置为允许跨域请求: 这是我的服务代码: [WebService(Namespace = "http://myservices.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] [ScriptService] public class Test : W

我有以下问题 我正在尝试使用jquery将HTML页面与asmx服务连接起来。 该服务位于我的本地IIS中,我已将其配置为允许跨域请求: 这是我的服务代码:

[WebService(Namespace = "http://myservices.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
public class Test : WebService
{   
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Employee TestMethod()
    {
        return new Employee { Id = 1, Name = "John"};
    }
}

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
      <webServices>
        <protocols>
          <add name="HttpGet"/>
          <add name="HttpPost"/>
        </protocols>
      </webServices>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
    <handlers>
      <add name="ScriptHandlerFactory"
           verb="*" path="*.asmx"
           type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           resourceType="Unspecified" />
    </handlers>
  </system.webServer>
</configuration>
这是我的web.config服务代码:

[WebService(Namespace = "http://myservices.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
public class Test : WebService
{   
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Employee TestMethod()
    {
        return new Employee { Id = 1, Name = "John"};
    }
}

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
      <webServices>
        <protocols>
          <add name="HttpGet"/>
          <add name="HttpPost"/>
        </protocols>
      </webServices>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
    <handlers>
      <add name="ScriptHandlerFactory"
           verb="*" path="*.asmx"
           type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           resourceType="Unspecified" />
    </handlers>
  </system.webServer>
</configuration>
我得到以下回应:

TestMethod?callback=jQuery220045970173459500074_1454084649739&[object object]&&u=1454084649740:1


未捕获SyntaxError:意外标记我认为在将响应发送回客户端之前,您没有正确序列化响应

必须将类序列化为Json字符串。要执行此操作,请遵循以下MSDN链接中提供的说明:

然后,您应该将web方法更改为返回字符串值,并且在将响应返回给客户端之前,执行序列化技巧,如下所示:

var objectSerializer = new JavascriptSerializer();
//serialization  
return objectSerializer.Serialize(data); //this will return a Json string
有关JavascriptSerializer类的更多详细信息,请查看以下链接:

关于您的错误,这是因为您的响应以XML格式(标准SOAP响应)返回,因此您使用的javascript代码需要Json格式。诀窍是简单地返回一个Json字符串

另一种选择是使用WCF而不是ASMX,但这是您可以在此处查看的另一条路径:

当心这样公开您的服务所涉及的安全风险。使用唯一的ID或其他机制来保护来自Javascript的web服务调用