0

我在 C# 中有一个 WebService,我想从另一个应用程序访问这个 WebService。前任。有一个在 localhost 中运行的 web 服务,我也有一个在 localhost 中运行的网站,这两个项目位于不同的地方。问题是:如何在本地主机中使用 ajax 从我的网站调用此 Web 服务。

我拥有的代码是这样的:WebService

[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    public String HelloWorld()
    {
        return "Hello World";
    }
}

和客户

$.ajax({
  type: "POST",
  url: "localhost:52137/Service1.asmx?op=HelloWorld",
  contentType: 'application/json; charset=utf-8',
  dataType: 'json',
  data: '',
  success: function (data, status) {
      alert(data.d);
  },
  error: function(data, status){
      alert(status);
  }
});
4

2 回答 2

0

由于这两个项目在不同的地方,这将是 CORS 请求。

您需要按照本文的建议在您的服务应用程序中启用跨域请求。

您可以使用允许跨域请求的$.getJSON 。

当您使用 C# 时,您可以创建 HTTP 处理程序,如本文所示。

于 2013-06-13T06:16:43.197 回答
0

尝试将 url 更改为此:

网址:“http://localhost:52137/Service1.asmx/HelloWorld”

顺便说一句..如果网站在与服务不同的端口上运行..您仍然有 xdomain 问题。

CORS ASMX

于 2013-06-13T03:36:17.980 回答