AJAX Http POST : 没有传输错误

AJAX Http POST : No transport error

这是我的网络方法:

[WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

这是 html 页面中的 AJAX 代码:

jQuery.support.cors = true;
                var btn_startOp = document.getElementById("startOpp");

                $.ajax({
                    type: "POST",
                    contentType: "application/xml; charset=utf-8",
                    data: "{}",  
                    dataType: "xml",
                    url: "http://localhost:61457/WebSite1/Service.asmx/HelloWorld",
                    success: function(msg){ alert(msg.d); },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(errorThrown);
                          }
                });

这给我一个错误:打开,

Webservice 在浏览器上测试时工作正常。

更新: 这是 Html 中 head 标签中的行:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

谁能帮我解决这个问题? 这是我的第一个代码..我搜索了很多对我来说仍然是个谜... :) :(

改为:

$.ajax({
  type: "GET",
  url: "/WebSite1/Service.asmx/HelloWorld",
  dataType:"json",
  success: function(msg) {
    alert(msg);
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert(errorThrown);
  }
});

  1. 您正在从 web 方法返回一个字符串值,因此 dataType:"xml" 不是必需的。
  2. Webservice 在浏览器上测试时工作正常。,这意味着您需要有一个 GET 请求。