将KnockoutJS Observable Array传递到HTTP Post Controller方法的AJax调用失败 [英] AJax Call that pass KnockoutJS Observable Array to the HTTP Post Controller Method fails

查看:51
本文介绍了将KnockoutJS Observable Array传递到HTTP Post Controller方法的AJax调用失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对Http Controller POST方法进行Ajax调用,从Ajax传递到Controller的数据是JSON数组,它是来自KnockoutJS的ObserverableArray.对HTTP控制器的AAX调用如下所示

I am trying to make a Ajax Call to the Http Controller POST method, the data passed from the ajax to Controller is the Array of JSON which is an ObserverableArray from KnockoutJS. The AAX Call to the HTTP Controller is like below

 $.ajax({
                    url: '/ORF/pdtInfo',
                    type: 'POST',
                    data: ko.toJSON(self.pdtDetails()),
                    success: function (result) {
                        console.log("OrderRequest - Recorded inserted Sucessfully" );
                    },
                    error: function (errorThrown) {
                        console.log("OrderRequest - Recorded insert Failed" +errorThrown.responseText );
                        callHandleUserError("Request Failed");
                    }
                })

传递给Controller的 ko.toJSON(self.pdtDetails())数据类似于

The data that is ko.toJSON(self.pdtDetails()), passed to Controller is like

[{"pdtNeededTypes":["Individual","Other"],
  "stockNumber":"0054654354  |  y8fdts-Tg(hhhjhj)2Mnn/J          [Also Known as : O2 , OygR4-EhaFP]",
  "pdtNeeded":"Other",
  "pdtTypes":""}]

Controller方法如下所示,该方法接收数组作为输入参数

The Controller method is like below which receives array as input parameter

   public JsonResult pdtInfo(List<Models.ORFPdtInfo> orfpdtInfo)
    {
        try
        {
            if (Session["ORFData"] != null)
            {
                ORFData ORFData = Session["ORFData"] as ORFData;
                ORFData.pdtInfo = orfpdtInfo;
                Session["ORFData"] = ORFData;
                var result = new JsonResult()
                {
                    Data = orfpdtInfo,
                    MaxJsonLength = Int32.MaxValue
                };
                return result;
            }
            else
            {
                return Json(new { redirectUrl = Url.Action("Index", "ORF"), isRedirect = true });
            }
        }
        catch (Exception e)
        {
            return null;
        }
    }

模型类ORFPdtInfo类似于

Where the Model Class ORFPdtInfo is like

public class ORFPdtInfo
{
    public List<string> pdtNeededTypes { get; set; }
    public string stockNumber { get; set; }
    public string pdtNeeded { get; set; }
    public string pdtTypes { get; set; }
}

ORFData是我在其中编写电子邮件内容的类,我将来自控制器的数据传递到此类中,用于编写电子邮件正文内容

And the ORFData is the class where I am composing the email content, from Controller I am passing the data in to this class for composing the email body content

[Serializable]
public class ORFData
{
public List<ORFPdtInfo> pdtInfo { get; set; }

public string SerializedOrderData()
{
    StringBuilder orderText = new StringBuilder();
    for (int i=0;i<pdtInfo.Count();i++ ){
    orderText.AppendLine("<tr><td width='30%'>Pdt Needed </td><td width='70%'>" + pdtInfo[i].pdtNeeded + "</td></tr>");
    .... 

问题在于,对Controller方法进行的Ajax调用会自动转到错误函数.我试图通过将错误放入控制台 errorThrown.responseText 来查看错误,但该错误为空.我还在控制器 List< Models.ORFPdtInfo>中设置了断点.orfpdtInfo 为null,没有任何内容从ajax调用传递给控制器​​,我是否缺少Model类绑定的任何内容.任何帮助将不胜感激

The issue is when the Ajax call that is made to the Controller method automatically goes to the error function. I tried to see the error by putting them in the console errorThrown.responseText but it is empty. I also set break point in the controller List<Models.ORFPdtInfo> orfpdtInfo is null,nothing is passed from the ajax call to the controller, am I missing anything with the Model class binding. Any help is greatly appreciated

推荐答案

在POST方法AJAX调用中添加 contentType:"application/json"

Adding the contentType: "application/json",to the POST method AJAX call fixed the issue

这篇关于将KnockoutJS Observable Array传递到HTTP Post Controller方法的AJax调用失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆