1

我在 IIS 7.5 中使用简单的 asmx Web 服务托管了 Web 应用程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace TestWebService
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class Test : System.Web.Services.WebService
    {
        [WebMethod]
        public static string HelloWorld()
        {
            return "Hello World";
        }

    }
}

当我单击调用按钮时,http://localhost/Test/Test.asmx?op=HelloWorld它工作正常,但是当我尝试使用 $.ajax() 从在 ASP.NET 开发服务器(http://localhost:1756/HTMLPage1.htm)上运行的不同 Web 应用程序项目中调用此方法时,我得到一个错误 response.status 为 0 和无法调用 web 方法,这是我的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Consume Test </title>
    <script src="jquery-1.5.1.js" type="text/javascript"></script>
    <script type="text/javascript">

        function displayMessageCall() {

            $.ajax({
                type: "POST",
                url: "http://localhost/Test/Test.asmx/HelloWorld",
                data: '{}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: successMsg,
                error: errorMsg

            });
        }

        function successMsg(response) {
            alert('Success');
        }

        function errorMsg(response) {
            alert(response.status + " " + response.statusText)
        }

        $(document).ready(function () {
            displayMessageCall();
        });
    </script>
</head>
<body>
</body>
</html>

我错过了什么吗?

4

1 回答 1

0

我认为您只是遇到了同源策略

由于端口差异,浏览器限制了 javascript 访问 web 服务。当然,也有一些解决方法

于 2012-09-03T09:24:54.780 回答