如何为每个jQuery JSON CSV设置

How to set foreach jQuery JSON CSV

本文关键字:JSON CSV 设置 jQuery      更新时间:2023-09-26

我有一个jquery ajax请求,如下所示;

$.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 文件为 as;

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

它回响;

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

我想提醒filename每个文件名。我该怎么做?

JSON 不是 CSV。这应该是一个错误。如果您正在接收 CSV,请不要说dataType: 'json',以文本形式接收,然后自己解析:

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

如果是 JSON,请编辑您的问题并澄清。

$.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]);
            }
       },
});

杰伦版本

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