如何用jQuery监听返回的json对象 [英] how to listen for returned json object with jquery

查看:117
本文介绍了如何用jQuery监听返回的json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,该页面的表单具有针对隐藏的iframe的文件上传输入项.将表单发布到iframe时,服务器处理文件并返回json对象.我不确定如何使用jquery或普通的旧javascript来侦听返回的对象.我为iframe设置了一些代码,例如...

I have a page with a form that has a file upload input item targeted to a hidden iframe. When the form is posted to the iframe the server processes the file and returns a json object. I'm not sure how to use jquery, or plain old javascript for that matter, to listen for the returned object. I have some code set up for my iframe such as...

$("#upload_target").load(function () {
   //what to do here - how do I get the json object?
});

有人知道如何连接jquery来侦听发送回iframe的json对象吗?谢谢.

Does anyone know how to wire up jquery to listen for the json object that's sent back to the iframe? Thanks.

推荐答案

我终于弄清楚了该怎么做....

I finally figured out how to do it....

$("#upload_target").load(function (data) {
    if (data != null){
        var obj = jQuery.parseJSON(data);
        //...work with obj here.
    }
});

无论方法是否正确,它都有效.

Whether it's the right way or not, it works.

编辑-实际上我有点领先.这是正确的代码....

edit - actually I got a bit ahead of myself. here's the correct code....

$("#upload_target").load(function (){
        var retval = $(frames['upload_target'].document).text();
        if (retval != null)
        {
            try{
                var obj = jQuery.parseJSON(retval);
                //...work with obj here.
            }catch(err){}
        }
});

我还必须更改的一件事是确保我的MVC控制器操作正在设置JSONResult.ContentType ="text/plain".否则,我会收到一个另存为下载"对话框.

One thing I had to also change was making sure that my MVC controller action was setting the JSONResult.ContentType = "text/plain". Otherwise I was getting a save as download dialog.

这篇关于如何用jQuery监听返回的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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