【问题标题】:asp.net http GET sending data by AJAXasp.net http GET 通过 AJAX 发送数据
【发布时间】:2018-02-07 20:28:11
【问题描述】:

我正在尝试从 AJAX GET 向我的控制器发送数据,但数据始终为空。

我的控制器:

    [Route("api/sendingData")]
public class myController : ApiController
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [HttpGet]
    public bool Get(dataModel data)
    {
        myFunc result = new myFunc(data);

        return result.success;
    }
}

我的数据模型:

    public class dataModel
{
    public int id { get; set; }
    public List<int> arrValues { get; set; } = null;
}

我通过html ajax调用控制器:

    data = {
    id: 1,
    arrValues : [40, 43]
};
$.ajax(
    {
        url: "api/sendingData",
        type: "GET",
        dataType: 'jsonp',
        data: data,
        success: function (result) {
            console.debug(result);
            alert(result);
        },
        error: function (xhr, status, p3, p4) {
            console.debug(xhr);
            var err = "Error " + " " + status + " " + p3;
            if (xhr.responseText && xhr.responseText[0] == "{")
                err = JSON.parse(xhr.responseText).message;
            alert(err);
        }
    });

当我调试控制器时,变量数据为空。为什么它没有从 AJAX 请求中获取数据?

谢谢

【问题讨论】:

  • 将你的 ajax 改成:"POST" 并用 HTTPPost 装饰你的后端方法,你正在向服务器发布数据,因此使用 "POST" 的原因
  • 我需要 GET 因为我需要使用 jsonp(跨服务器)
  • 此控制器是否与您发布数据的网站不在同一台服务器上?
  • 没有。数据来自 ColdFusion 服务器,控制器位于 Azure 服务器中
  • 为什么不直接启用 CORS,这看起来像是发生了什么......

标签: jquery asp.net ajax controller


【解决方案1】:

尝试输入:“POST”。 当您输入:“GET”时,您的日期将进入您的网址,如下所示:

api/sendingData?id=1&arrValues=value

【讨论】:

  • 我需要get,因为我需要jsonp(跨服务器)
【解决方案2】:

模型不需要在 dataModel 类上分配 null。

public class dataModel
{
    public int id { get; set; }
    public List<int> arrValues { get; set; } 
}

更改您的控制器以接受 int 和 string 等原始类型,因为获取请求,然后使用查询字符串发送数据以从 ajax 获取请求。

[Route("api/sendingData")]
        public class myController : ApiController
        {
            [EnableCors(origins: "*", headers: "*", methods: "*")]
            [HttPGet]
            public bool Get(int id, string commaSeparatedValues)
            {
                var data = new dataModel { id = id, arrValues = commaSeparatedValues.Split(',').Select(x => Int32.Parse(x)).ToList()};
                myFunc result = new myFunc(data);

                return result.success;
            }
        }


$.ajax(
    {
        url: "api/sendingData?id="+1+"&arrValues='1,2,3,4'",
        type: "GET",
        dataType: 'jsonp',
        data: data,
        success: function (result) {
            console.debug(result);
            alert(result);
        },
        error: function (xhr, status, p3, p4) {
            console.debug(xhr);
            var err = "Error " + " " + status + " " + p3;
            if (xhr.responseText && xhr.responseText[0] == "{")
                err = JSON.parse(xhr.responseText).message;
            alert(err);
        }
    });

【讨论】:

  • 我需要 GET 因为我需要使用 jsonp(跨服务器)
  • 好的,现在更新答案,5分钟后发布。
  • 它返回“加载失败的
  • 您的请求是什么样的?您可以从 chrome 开发者工具(网络选项卡)发送请求 URL 吗?
  • 使用 Firefox 会返回:源为“myServer/api/…”的
猜你喜欢
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
相关资源
最近更新 更多