【问题标题】:Ajax get data from Web Service in XML format when specified as JSON当指定为 JSON 时,Ajax 以 XML 格式从 Web 服务获取数据
【发布时间】:2021-03-31 17:04:03
【问题描述】:

我正在尝试使用 ajax 调用从 Web 服务中获取数据,其函数的返回值为 json 格式的字符串。

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyClass : WebService {
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]

    public Chart RetrieveData(string countries, string startDate, string endDate)
    {
        Chart c = getData(countries, startDate, endDate);
        return c;
    }

}

我正在使用 Ajax,如下所示:

var path = 'pathToXml/file.asmx/RetrieveData?countries=' + countries + '&startDate=' + startDate + '&endDate=' + endDate;
$.ajax({
    type: "GET",
    url: path,
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        console.log(data.d);
    },
    error: function (textStatus, errorThrown) {
        console.log(textStatus);
        console.log(errorThrown);
    }
});

我已经阅读了诸如 thisthis 这样的帖子。但是,这些都不起作用

【问题讨论】:

  • 您需要访问data.dmsg.d 值...stackoverflow.com/a/36822653/4000335
  • @kshkarin 对于我的第一个 ajax 调用,无论我是否将数据类型指定为“文本”,data.d 都会给我“欠精细”。而我的第二个 ajax 只是有错误,所以我认为这不会起作用。

标签: c# jquery .net ajax


【解决方案1】:

ScriptMethod 属性中需要UseHttpGet = true,否则会出现此错误:

尝试使用 GET 请求调用方法“RetrieveData”,这是不允许的。

MyWebService.asmx

<%@ WebService Language="C#" Class="MyWebService" %>

using System.Web.Script.Services;
using System.Web.Services;

[ScriptService]
public class MyWebService : WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
    public object RetrieveData(string countries, string startDate, string endDate)
    {
        return new
        {
            countries,
            startDate,
            endDate
        };
    }
}

index.html

<!DOCTYPE html>
<html>
<body>
<button onclick="retrieveData('usa','6/18/2012','4/1/2021')" type="button">Retrieve Data</button>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
        integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
        crossorigin="anonymous"></script>
<script>
    function retrieveData(countries, startDate, endDate) {
        const path = `/MyWebService.asmx/RetrieveData?countries='${countries}'&startDate='${startDate}'&endDate='${endDate}'`;
        $.ajax({
            type: 'GET',
            url: path,
            contentType: 'application/json; charset=utf-8',
            success: function(data) {
                console.log(data.d);
            }
        });
    }
</script>
</body>
</html>

【讨论】:

  • 我没有将数据发布到服务器,而是从服务器取回数据。
  • @AngryOtter 示例已更新为 GET。为 id 和 UseHttpGet = true 添加了查询字符串参数添加到 ScriptMethod 属性。
  • 它不起作用。您提供的解决方案类似于我在问题中链接的帖子中的解决方案。通过使用您的示例,我得到:An attempt was made to call the method 'RetrieveChart' using a GET request, which is not allowed。如果我更改为 post,它会说我缺少参数值。
  • @AngryOtter 清理了工作示例,使其看起来像您开始的那样,删除了我的额外示例。
  • 我已经清理了我的示例。我知道如何在 XML 节点中获取 json 字符串,但我希望 Web 服务返回实际的 JSON 对象而不是 XMLdocument,并将 Json 字符串作为节点值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-08
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
相关资源
最近更新 更多