2012-05-12 7 views
1

私はjquery ajaxリクエストを以下のようにしています。foreach jQueryを設定する方法JSON CSV

$.ajax({ 
     type: 'POST', 
     url: 'ajax.php', 
     dataType: 'json', 
     cache: false, 
     success: function(result) { 
     alert (result); 
     }, 
}); 

返されたresultはJSONにあります。データを見るために、私はそれを警告した。

img1.jpg,img2.jpg,img3.jpg,img4.jpg 

phpファイルは次のとおりです。

<?php 
$array = array("img1.jpg","img2.jpg","img3.jpg","img4.jpg"); 
echo json_encode($array); 
?> 

echos;

["img1.jpg","img2.jpg","img3.jpg","img4.jpg"] 

それぞれのファイル名にはfilenameを通知したいと思います。これどうやってするの?

答えて

1
$.ajax({ 
     type: 'POST', 
     url: 'ajax.php', 
     dataType: 'text', 
     cache: false, 
     success: function(result) { 
      var filenames = result.split(','); 
      for(i=0;i<filenames.length;i++) 
      { 
       alert(filenames[i]); 
      } 
     }, 
}); 

JSONバージョン

$.ajax({ 
      type: 'POST', 
      url: 'ajax.php', 
      dataType: 'json', 
      cache: false, 
      success: function(result) { 
       $.each(result, function(key,val){ 
        alert(val); 
       }); 
      }, 
    }); 
+0

私の質問は余分な詳細を変更していただきありがとうございます。ありがとうございました –

4

JSONはCSVではありません。これはエラーです。あなたはCSVを受けている場合はdataType: 'json'を言っていない、テキストとしてそれを受け取り、それを自分で解析:

var fields = result.split('/'); 
$.each(fields, function(fieldNo, field) { 
    alert(field); 
}); 

それがJSONである場合は、あなたの質問を編集して明確にします。

+0

は、余分な詳細を私の質問を変更しました。ありがとう –

0
$.ajax({ 
     type: 'POST', 
     url: 'ajax.php', 
     dataType: 'json', 
     cache: false, 
     success: function(result) { 
      $.each(result,function(keu,value){ 
       alert(value); 
      }); 
     }, 
}); 
+0

'file1.ext、file2.ext、file3.ext'で' result'と一緒に動作していません –

+0

あなたの結果はfile1.ext、file2.ext、file3.extではありません。 –

+0

私の質問は余分な内容で修正されました。 –

関連する問題