通过AJAX将JSON导入我的控制器:我缺少什么?

Getting JSON into my controller through AJAX: What am I missing?
2020-10-18
  •  译文(汉语)
  •  原文(英语)

我有

    // fbi: Files batch info

    $.ajax({
        xhr: function() {
            var xhr = new window.XMLHttpRequest();
            return xhr;
        },
        type: 'POST',
        url: '@Url.Action("AddFilesToDB","FileUploadAsync")',
        data:  JSON.stringify(fbi),
        contentType: false,
        processData: false,
        success: function(retInfo) {
            console.log(JSON.stringify(retInfo));
        }
    });

我如何引用JSON.stringify的结果,像

'{org:"someOrganization",cat:"someCategory",fileNames:[somepic.jpg,somepaper.docx]'

从我的控制器内部

[HttpPost]
public async Task<JsonResult> addFilesToDB (???)
{
    ??? Want to use the value of data
}

我知道这可能是一个显而易见的问题,但是我是新手

速聊1:
创建性能模型string Org,string Catstring[] Filenames因此该模型将与您的数据绑定.
速聊2:
并且该模型类型的元素应该作为函数的参数吗?
速聊3:
对此进行检查,这可能对您有帮助codepedia.info/2015/02/…
速聊4:
,该方法将是public async Task<JsonResult> addFilesToDB (yourModel model).另请注意,您不需要.stringify()- data: fbi,应该可以工作
速聊5:
爱德华·斯诺登会知道这样的事情>.
解决过程1

解决方法在这里,首先创建一个模型 FileData

public class FileData
{
    public string org { get; set; }
    public string cat { get; set; }
    public string[] filenames { get; set; }    
}

然后创建如下所示的动作-

public JsonResult PostFileData(FileData filedata)
{
     return Json("True");
}  

然后,在视图中,您的JQuery代码应为-

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    $(function () {
        $("#btnSubmit").click(function () {
            var roles = ["somepic.jpg", "somepaper.docx"];

            var model = new Object();
            model.fileNames = roles;
            model.cat = "business";
            model.org = "news";

            jQuery.ajax({
                type: "POST",
                url: "@Url.Action("PostFileData", "Home")",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ filedata: model }),
                success: function (data) { alert(data); },
                failure: function (errMsg) {
                    alert(errMsg);
                }
            });
        });
    });
</script>

<input type="button" value="Click" id="btnSubmit" />

当您点击时btnSubmit,所有值都会发布到上述操作中,并带有一个断点,如下所示-

通过AJAX将JSON导入我的控制器:我缺少什么?

I have

    // fbi: Files batch info

    $.ajax({
        xhr: function() {
            var xhr = new window.XMLHttpRequest();
            return xhr;
        },
        type: 'POST',
        url: '@Url.Action("AddFilesToDB","FileUploadAsync")',
        data:  JSON.stringify(fbi),
        contentType: false,
        processData: false,
        success: function(retInfo) {
            console.log(JSON.stringify(retInfo));
        }
    });

How do I refer to the result of JSON.stringify, an object like

'{org:"someOrganization",cat:"someCategory",fileNames:[somepic.jpg,somepaper.docx]'

from inside my controller

[HttpPost]
public async Task<JsonResult> addFilesToDB (???)
{
    ??? Want to use the value of data
}

I know this is probably an obvious question, but I'm new to this stuff

Talk1:
Create a model with properties string Org, string Cat and string[] Filenames so the model will be bound with your data.
Talk2:
And should an element of that model type be the parameter to the function?
Talk3:
Check this, it may be useful for you codepedia.info/2015/02/…
Talk4:
, the method would then be public async Task<JsonResult> addFilesToDB (yourModel model). Note also you do not need .stringify() - just data: fbi, should work
Talk5:
Edward Snowden would know something like this >.>
Solutions1

Here goes the solution, first create a model FileData

public class FileData
{
    public string org { get; set; }
    public string cat { get; set; }
    public string[] filenames { get; set; }    
}

Then create an action as shown below -

public JsonResult PostFileData(FileData filedata)
{
     return Json("True");
}  

Then in the view your JQuery code should be -

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    $(function () {
        $("#btnSubmit").click(function () {
            var roles = ["somepic.jpg", "somepaper.docx"];

            var model = new Object();
            model.fileNames = roles;
            model.cat = "business";
            model.org = "news";

            jQuery.ajax({
                type: "POST",
                url: "@Url.Action("PostFileData", "Home")",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ filedata: model }),
                success: function (data) { alert(data); },
                failure: function (errMsg) {
                    alert(errMsg);
                }
            });
        });
    });
</script>

<input type="button" value="Click" id="btnSubmit" />

When you click on the btnSubmit, all the values will be posted to the above action and with a breakpoint it should be like shown below -

Getting JSON into my controller through AJAX: What am I missing?

转载于:https://stackoverflow.com/questions/30793445/getting-json-into-my-controller-through-ajax-what-am-i-missing

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入