繁体   English   中英

MVC4 AJAX发布JSON

[英]MVC4 AJAX post JSON

这是我的功能,我只发布json

function test() {
            var imgFile = document.getElementById('image');
            //            var imgData = JSON.stringify(getBase64Image(imgElem));
            //var imgData = Convert.FormBase64String(imgElem);

            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: "http://localhost:59102/Contacts/AddContact",
                data: "json=" + "{\"token\":\"8mVm/nS1OfpU+nlQLbJjqXJ7kJI=VyLGI2GEKkGgtDt0babrAw==\"}",

                success: function (returnPayload) {
                    console && console.log("request succeeded");
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    console && console.log("request failed");
                },

                processData: false,
                async: false
            });

而且我不知道如何添加到我的数据,图像中,我需要发布json和图像

这是我的控制器

[HttpPost]
        [AllowAnonymous]
        public JsonResult AddContact(string json, HttpPostedFileBase file)
        {}

除非使用使用Flash或iframe等其他“技术”的插件,否则您无法通过AJAX上传文件(按设计)-这是一种安全措施,因为JavaScript读取计算机上的本地文件不是最佳方法

这里有一个选项: http : //jquery.malsup.com/form/

...否则,我建议您查找其他多种选择之一!

在将数据获取到base 64之后,您的json应该是控制器中的对象,而json应该是这样的

{"json":"something here that is a string","file":"some file"}

同样在客户端,您应该有一个对象,您可以在该对象上调用JSON.stringify()

var ob = {json:imageDataAsBase64,file:fileDataAsBinary}

尽管我看不到发送两者的理由。 如果您只需要传输图像,则只需将图像获取为base64并将其发布为json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM