19

我试图从 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?";
    }
}
4

8 回答 8

27

突出的一件事是你有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>
于 2009-05-18T19:32:43.077 回答
6

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

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string GetSurvey() {
    return "Question: Who is Snoopy?";
}
于 2009-05-18T20:11:04.497 回答
2

这是一个 jQuery 调用 aspx 上的页面方法的示例,但它类似于 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

    });
于 2009-05-18T19:33:39.130 回答
2

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

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

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

[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?";
    }
}
于 2012-06-15T13:57:18.653 回答
1

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

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

error: function(objXMLHttpRequest, textStatus, errorThrown) {
 debugger;               
}
于 2009-05-18T19:42:25.150 回答
1

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

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

使用 GET 有很多正当理由。

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

于 2012-06-08T17:04:11.830 回答
1

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

  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>

于 2017-08-13T18:41:07.310 回答
0

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

于 2016-10-10T19:34:57.390 回答