George_Fal 2016-09-02 10:57 采纳率: 0%
浏览 20

解析Javascript对象

I am using dropzone.js to upload files via ajax but I need to implement a fallback method for older browsers that don't support XMLHttpRequest.

To work around this I'm using the jQuery Iframe Transport plugin

 $.ajax({
        url: "/File/Create",
        type: 'POST',
        cache: false,
        data: { fallback: true },
        files: $("form :file"),
        iframe: true,
        processData: false
   }).complete(function (data) {
        console.log(data.message);

    });

As the jquery iframe transport does not have access to the HTTP headers of the server response, it is not as simple to make use of the automatic content type detection provided by jQuery as with regular XHR.

A workaround is to send back an HTML document containing just a <textarea> element with a data-type attribute that specifies the MIME type, and put the actual payload in the textarea:

<textarea data-type="application/json">
   {"ok": true, "message": "Thanks so much"}
</textarea>

So in my asp mvc controller I return the response as follows:

if (fallback == "true")
{
    return Content("<textarea data-type=\"application/json\">{\"ok\": false, \"message\": \"Success.\"}</textarea>");

}

Which returns the following javascript object:

Object { readyState=4, responseText="{"ok": false, "message": "Success"}", responseJSON Object {ok=false, message="Success"}

How do I parse this response to output the Json message. The following gives me undefined?

.complete(function (data) {
        console.log(data.message);
});
  • 写回答

1条回答 默认 最新

  • weixin_33716941 2016-09-05 08:37
    关注

    Figured it out. just needed to do the following:

    console.log(data.responseJSON.message);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用