【问题标题】:Calling ASMX from jQuery从 jQuery 调用 ASMX
【发布时间】:2010-10-27 03:12:34
【问题描述】:

我试图从 jQuery 调用 ASMX 方法但没有成功。以下是我的代码,我不明白我缺少什么。

文件Something.js,

function setQuestion() {
    $.ajax({
        type: "POST",
        data: "{}",
        dataType: "json",
        url: "http: //localhost/BoATransformation/Survey.asmx/GetSurvey",
        contentType: "application/json; charset=utf-8",
        success: onSuccess
    });
}

function onSuccess(msg) {
    $("#questionCxt").append(msg);
}

文件 SomethingElse.cs,

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Survey : System.Web.Services.WebService {

    public Survey () {
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public string GetSurvey() {
        return "Question: Who is Snoopy?";
    }
}

【问题讨论】:

    标签: c# jquery asmx


    【解决方案1】:

    突出的一点是您拥有UseHttpGet=true,但在您的 jQuery 代码中您使用的是 POST。

    这里还有一个我创建的调用 ASMX 页面的测试页面。

    [WebMethod]
    public Catalog[] GetCatalog()
    {
        Catalog[] catalog = new Catalog[1];
        Catalog cat = new Catalog();
        cat.Author = "Jim";
        cat.BookName ="His Book";
        catalog.SetValue(cat, 0);
        return catalog;
    }
    
    <script type="text/javascript">
        $(document).ready(function() {
        $.ajax({
                type: "POST",
                url: "default.asmx/GetCatalog",
                cache: false,
                contentType: "application/json; charset=utf-8",
                data: "{}",
                dataType: "json",
                success: handleHtml,
                error: ajaxFailed
            });
        });
    
        function handleHtml(data, status) {
            for (var count in data.d) {
                alert(data.d[count].Author);
                alert(data.d[count].BookName);
            }
        }
    
        function ajaxFailed(xmlRequest) {
            alert(xmlRequest.status + ' \n\r ' + 
                  xmlRequest.statusText + '\n\r' + 
                  xmlRequest.responseText);
        }
    </script>
    

    【讨论】:

    • 据我了解,我自己也在尝试做类似的事情。你能告诉我,因为[WebMethod] 属性似乎在常规 ASPX 文件中受支持,为什么需要一个 ASMX 文件?谢谢。
    • 乔纳森,如果您正在创建一个仅用于特定页面的方法,那么您可以直接将其添加到页面中。但是,如果您想在多个页面中使用它,最好为它创建一个专用的 asmx,以便它可以重复使用并且与某些页面没有直接关系。
    • 所以我在服务器的 IIS 中托管了 ASMX,ASMX 有一个函数DoTest,我可以点击它,它会将我带到另一个页面,我可以点击“Invoke”按钮然后它显示 XML。但是,当我尝试使用 JQuery 调用进行上述操作时,我得到一个 0 and error 作为 ajaxFailed 错误。任何想法/
    【解决方案2】:

    如果这是您想要的,您必须确保将 Json 指定为响应格式,并且由于 security features 而摆脱 UseHttpGet

    [WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public string GetSurvey() {
        return "Question: Who is Snoopy?";
    }
    

    【讨论】:

      【解决方案3】:

      这是一个对 aspx 页面方法的 jQuery 调用示例,但它类似于 asmx 页面。

      $.ajax(
          {
              type: "POST",
              url: "NDQA.aspx/ValidateRoleName",
              data: '{"roleName":"' + $('[id$=RoleNameTextBox]').val() + '"}',
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: ValidateSuccess,
              error: ValidateError
      
          });
      

      【讨论】:

        【解决方案4】:

        我遇到了这个问题并且遇到了同样的问题。我通过添加解决了它:

        [WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
        

        如果您想使用 POST,请在您的网络方法属性下方。即:

        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.Web.Script.Services.ScriptService]
        public class Survey : System.Web.Services.WebService {
        
            public Survey () {
            }
        
            [WebMethod]
            [WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
            [ScriptMethod(UseHttpGet = true)]
            public string GetSurvey() {
                return "Question: Who is Snoopy?";
            }
        }
        

        【讨论】:

          【解决方案5】:

          我还建议按照 Jim Scott 的建议删除 UseHttpGet。

          您可以将以下内容添加到您的选项中并检查 objXMLHttpRequest 以查看更详细的错误响应。

          error: function(objXMLHttpRequest, textStatus, errorThrown) {
           debugger;               
          }
          

          【讨论】:

            【解决方案6】:

            如果这是您想要的,您必须确保将 Json 指定为响应格式,并且由于安全功能而摆脱 UseHttpGet:

            如果您阅读了那篇文章,那么您会发现使用 UseHttpGet 是安全的,因为 ASP.NET 具有阻止跨站点脚本攻击向量的功能。

            使用 GET 有很多正当理由。

            他可以删除数据参数并将 POST 更改为 GET 以使调用正常工作。假设您需要 JSON 响应,则还需要添加 ResponseFormat=ResponseFormat.Json。

            【讨论】:

              【解决方案7】:

              以下步骤解决了我的问题,希望对某人有所帮助,

              1. 要允许使用 ASP.NET AJAX 从脚本调用此 Web 服务,请在 asmx 服务类上方添加以下行,例如

                [System.Web.Script.Services.ScriptService] 公共类 GetData:System.Web.Services.WebService {

              2. 在web.config中system.web下添加协议,如果无法查看配置请点击链接

              https://pastebin.com/CbhjsXZj

              <system.web>
              <webServices>
                <protocols>
                  <add name="HttpGet"/>
                  <add name="HttpPost"/>
                </protocols>
              </webServices>
              

              【讨论】:

                【解决方案8】:

                如果您尝试使用 chrome 浏览器,请尝试使用 Internet Explorer,它对我有用,而且它与 chrome 浏览器有关,您必须添加扩展以在 chrome 中工作,但我不知道扩展名

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2011-03-31
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-01-25
                  • 2011-04-05
                  相关资源
                  最近更新 更多