1

jQuery のアップロード プラグインを使用しようとしました。 http://valums.com/ajax-upload/

返される応答タイプを json に設定すると、Firefox は返される json オブジェクトをどのように処理するかを尋ねるダイアログをポップアップ表示します。

アップロード スクリプトの作成者のページで同じ質問が寄せられていますが、今のところ回答はありません。願わくば、ここにいる JavaScript 担当者が、これを処理する方法を理解してくれることを願っています。

ありがとう。

<script type= "text/javascript">
      /*<![CDATA[*/
        $(document).ready(function(){

            /* example 1 */
            var button = $('#button1'), interval;
            new AjaxUpload(button, {
                //action: 'upload-test.php', // I disabled uploads in this example for security reasons
                action: '/posts/upload_images/', 
                name: 'myfile',
                responseType: 'json',
                onSubmit : function(file, ext){
                    // change button text, when user selects file           
                    button.text('Uploading');

                    // If you want to allow uploading only 1 file at time,
                    // you can disable upload button
                    this.disable();

                    // Uploding -> Uploading. -> Uploading...
                    interval = window.setInterval(function(){
                        var text = button.text();
                        if (text.length < 13){
                            button.text(text + '.');                    
                        } else {
                            button.text('Uploading');               
                        }
                    }, 200);
                },
                onComplete: function(file, response){
                    var json = response;
                    alert(json);
                    button.text('Upload');

                    window.clearInterval(interval);

                    // enable upload button
                    this.enable();

                    // add file to the list
//                    $('<li></li>').appendTo('#example1 .files').text(json.response_text);                     
                    $('<li></li>').appendTo('#example1 .files').text(file);                     
                }
            });
        });
    /*]]>*/
</script>
4

4 に答える 4

2

http://api.jquery.com/jQuery.parseJSON/

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );
于 2010-11-10T06:11:15.923 に答える
1

この jQuery プラグインにより、JSON との変換が簡単になります: http://code.google.com/p/jquery-json/

また、参照したブログ投稿の次のコメントにも興味があるかもしれません。

ブログ投稿をスパムして申し訳ありませんが (これは素晴らしいことです)、問題を発見したことをお伝えしたいと思います:

何らかの理由で、応答<pre>のタイプが の場合、応答には常に応答全体の周りにタグがありますplain/text。それが原因で、eval()呼び出しが失敗しました。eval()私の現在の解決策は、通話の前にこれらのタグを取り除くことでしたが、今ではすべてが機能しています。素晴らしい解決策ではありませんが、少なくとも今のところ作業を続けることができます。

于 2009-05-02T17:36:28.940 に答える
0

同じスクリプトの解決策を探していて、このページに出くわしました。オンラインで解決策が見つからなかったので、修正方法は次のとおりです。

@ upload-file.php: 置換

echo "success".$cc; 

echo json_encode(array(
status' => 'success',
'id' => $picid,
'image' => $imgurl
)); 

@ front end: 置換

var bb=response.substr(0,7)
var idd=response.replace('success',' ');
var idb =idd.replace(/^\s*|\s*$/g,'');
if(bb==="success")
{
    $('<span></span>').appendTo('#files').html('<img src="images/'+file+'" alt="" width="120" height="120" style="margin:5px;" />').addClass('success');
}
else
{
    $('<span></span>').appendTo('#files').text(file).addClass('error');
}

var what = jQuery.parseJSON(response);
if(what.status == 'success')
{
    $('<span id='+what.id+'></span>').appendTo('#files').html('<img src="'+what.image+'" alt="" width="120" height="120" style="margin:5px;" /><br><a href="javascript:void(0)" onClick="deleteFile('+what.id+');">Delete</a>').addClass('success');
}
else 
{
    $('<span></span>').appendTo('#files').text(response).addClass('error');
}

そして実際にこの質問に答えるために。

jQuery.parseJSON(response);

します..

于 2012-06-26T23:50:41.013 に答える
0

私はそのプラグインについて何も知らないのでわかりませんが、サーバー側で設定している応答タイプを確認する必要があるかもしれません。「text/plain」、「text/javascript」、「application/javascript」などのコンテンツ/MIME タイプを持つように HTTP 応答を設定する必要があります。これで問題が解決するかどうかを確認してください。

于 2009-05-02T17:39:40.913 に答える