【问题标题】:Posting JSON data to cross domain using ajax使用 ajax 将 JSON 数据发布到跨域
【发布时间】:2017-01-02 05:13:02
【问题描述】:

我正在开发基于 Web 表单的 Web 应用程序表单,我想将 JSON 数据发送到一个 MVC 应用程序。两者都在不同的域上。

这是我的ajax方法:

$.ajax({
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    url: 'http://localhost:52099/Controller/Index',   
    data: JSON.stringify(response.d),
    dataType: 'jsonp',
    success: function (response) {
        alert("done");
    },
    error: function(e){
        alert(e);
    }
});

这是我的操作方法:

[HttpGet]
    public ActionResult Index(ClientInformation model)
    {
        return(View);
    }

Index 方法被调用,但我没有得到参数值。我需要做什么?这里有什么不对吗?

这是我作为 response.d 的数据:

[{"Id":50345520,"FirstName":"Matthew","LastName":"McCauley","MiddleName":"","Suffix":null,"NPI":"1083043491","Address":"614 Esplanade St.","City":"Piscataway","State":"NJ","ZIP":"08854","Country":"United States","SubscriberFirstName":null,"SubscriberLastName":null,"SubscriberMiddleName":null,"SubscriberSSN":null,"SubscriberDOB":null,"SubscriberGender":null,"SubscriberSuffix":null,"SubscriberAddress":null,"SubscriberCity":null,"SubscriberState":null,"SubscriberZIP":null,"SubscriberCountry":null,"VisitDate":"\/Date(1483295400000)\/"}]"

还有

public class ClientInformation
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public string Suffix { get; set; }
    public string NPI { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZIP { get; set; }
    public string Country { get; set; }
    public string SubscriberFirstName { get; set; }
    public string SubscriberLastName { get; set; }
    public string SubscriberMiddleName { get; set; }
    public string SubscriberSSN { get; set; }
    public DateTime? SubscriberDOB { get; set; }
    public int SubscriberGender { get; set; }
    public string SubscriberSuffix { get; set; }
    public string SubscriberAddress { get; set; }
    public string SubscriberCity { get; set; }
    public string SubscriberState { get; set; }
    public string SubscriberZIP { get; set; }
    public string SubscriberCountry { get; set; }
    public DateTime? VisitDate { get; set; }
}

【问题讨论】:

  • 什么是response.d
  • 其实这个ajax在另一个ajax调用成功下。在 response.d 我得到 json 数据。
  • 对于初学者,您需要为跨源请求实现 CORS 服务器端。不难研究如何实现它
  • 我需要做什么?
  • response.d 里面有数据吗?你确定,试着做一个警报。如果您确定,请显示 response.d json 和 ClientInformation 的代码

标签: jquery json ajax asp.net-mvc


【解决方案1】:

我可以看到您的 response.d 是一个数组,因此要在您的控制器操作方法上接收此数据,您需要使用一个列表:

[HttpPost]
public ActionResult Index(List<ClientInformation> model)
{
    return(View);
}

我注意到的另一件事是,您正在发送一个发布请求表单 ajax,并且在您使用 HttpGet 的操作方法上,请将其更改为 HttpPost。

现在将 ClientInformation 模型更改为 List 模型后,您将在模型对象中获得数组 response.d 的完整数据。

我通过向上述操作方法发送以下 ajax 请求在我的应用程序中进行了尝试,我能够正确接收所有数据:

<script>
        $(document).ready(function () {
        var r = [{ "Id": 50345520, "FirstName": "Matthew", "LastName": "McCauley", "MiddleName": "", "Suffix": null, "NPI": "1083043491", "Address": "614 Esplanade St.", "City": "Piscataway", "State": "NJ", "ZIP": "08854", "Country": "United States", "SubscriberFirstName": null, "SubscriberLastName": null, "SubscriberMiddleName": null, "SubscriberSSN": null, "SubscriberDOB": null, "SubscriberGender": null, "SubscriberSuffix": null, "SubscriberAddress": null, "SubscriberCity": null, "SubscriberState": null, "SubscriberZIP": null, "SubscriberCountry": null, "VisitDate": "\/Date(1483295400000)\/" }];
        $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: '/Employee/Test',
            data: JSON.stringify(r),
            dataType: 'jsonp',
            success: function (response) {
                alert("done");
            },
            error: function (e) {
                alert(e);
            }
        });

        });
    </script>

【讨论】:

  • 感谢您的回复。但是 ajax 调用不会调用我的 post 方法。知道为什么吗?
  • 那么这可能是任何其他问题.. 更改为 List 后,您是否通过 HttpGet 请求获取此数据?正如您在帖子中提到的那样,正在调用操作方法但数据为空? @Ankita
  • 不,我没有在 GET 方法中得到它。
  • 当您将您的操作方法更改为 HttpPost 时,您的操作方法是否正在调用? @Ankita
  • 对我来说一切正常,我能够在我的列表中正确接收数据。您可以在我的帖子中看到我的 $.ajax 代码和操作方法代码。 @Ankita
猜你喜欢
  • 2011-01-02
  • 2016-07-20
  • 2013-03-18
  • 1970-01-01
  • 2012-07-12
  • 2012-06-05
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多