iT邦幫忙

0

php用file_put_contents Ajax無法接收資料

  • 分享至 

  • xImage

如題
有加不能接收
https://ithelp.ithome.com.tw/upload/images/20210630/20129366GPNY7dSGu3.png
沒加就可以
https://ithelp.ithome.com.tw/upload/images/20210630/2012936609bRdnm7tG.png
以下php

<?php
header('Content-Type: application/json; charset=UTF-8');
include 'jsonFormat.php';

if(isset($_POST["filename"])){
    $filename =$_POST["filename"];

    $wherefile='';
    //切割字串成陣列
    $fileNameCmps = explode(".", $filename);
     // 取副檔名
    $fileExtension = strtolower(end($fileNameCmps));
   
    if($fileExtension=='apk'){
        $wherefile='apkfile/';
      }else{
        $wherefile='ipafile/';
      }
    
    $filepath = "uploaded_files/".$wherefile. $filename;
  
    if(file_exists($filepath)){
        
        // unlink($filepath);
    
        // 開啟json檔
        $data=file_get_contents('record.json');
        $list = json_decode($data,true); 
        // $list=$list['filename'];
        // echo json_encode($list[0]['data'][0]['listname']);
       
        // 找出選到要刪除的資料

        for($i=0;$i<count($list['filename']);$i++){             
            for($j=0;$j<count($list['filename'][$i]['data']);$j++){
                if($list['filename'][$i]['data'][$j]['listname']==$filename){
                    if(count($list['filename'][$i]['data'])==1){   
                    // 用unset 會讓array型態變object
                    unset($list['filename'][$i]); 
                    // 用sort校正回array
                    sort($list['filename']);
                    }else{            
                    unset($list['filename'][$i]['data'][$j]); 
                    sort($list['filename'][$i]['data']);
                    }
                    break;
                }
            }
        }       
        // file_put_contents("record.json", jsonFormat($list));
        
        echo json_encode('okk');
    }
}

以下為ajax

$(document).ready(function () {
    $('.del').each(function () {
        $(this).click(function () {
            let filename=$(this).parent().parent().find('.name').html()
       
            $.ajax({
                type: "POST",
                url: "delete.php",
                dataType:"json", 
                data: { 
                   filename:filename,
                },
                success: function(data) {
                    console.log(data)
                   
                },
                error: function(jqXHR) {
                    console.log("沒回啦")
                    // window.location.reload();
                }
            })
        })
    })
});

有大大提供解決辦法嗎?
感謝!!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
㊣浩瀚星空㊣
iT邦大神 1 級 ‧ 2021-06-30 22:07:13
最佳解答

會跑到 error 事件,代表你的請求一定發生了什麼問題。
你該要先知道,你是發生了什麼錯誤。
而不是亂改程式。

error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.log(XMLHttpRequest.responseText);
                    },

將錯誤事件改用上面的程式碼。
可以在console上看到你發生了什麼錯誤。

在來,就是通靈的階段了。
這邊看不到你的 jsonFormat 這個函式做了什麼處理。
我猜測這裏輸出的東西,並非是文字型態。

因為 file_put_contents 是需要純文字才能寫入的。

再來,其實我明白 file_put_contents 這個真的很方便。
但它很容易有鎖檔及權限的問題。
如果是在WINDOWS系統上使用的話。個人經驗很常發生問題。
一般我還是比較喜歡用fopen來處理。至少發生問題我還可以知道發生什麼事就是了。
當然這是個人感覺問題啦。也可能遇到太多抗了。所以現在都不太喜歡用 file_put_contents
但 file_get_contents 也是很常用就是了。畢竟他比用fopen ,還要不容易發生搶檔問題。

大大您好
您的error log 有幫助到我解決問題了
jsonFormat這是整理json格式 讓自己方便閱讀
結果是我的jsonFormat裡面有個函數沒定義跳警告
不過 file_put_contents 似乎沒受這個警告影響都正常動作
警告基本不是不會影響程式運行 怎麼會影響到ajax的傳送這我滿好奇的

因為警告會輸出內容。會破壞掉AJAX原本要接收的JSON格式的東西。
基本你那段AJAX,如果收到非JSON格式的都會直接報錯。

用我那段可以直接看錯誤訊息或是輸出訊息。
也可以先暫時將 dataType 先改成html看看也行。

重點是找問題要對症下藥。不要自已那邊又猜又改半天。

大大提供error log時問題已解
ajax那個是多問的,因為看起來像是file_put_contents程式執行完 跳警告 中斷 然後echo json_encode('okk');就不執行
所以好奇這警告會造成程式的中斷是怎麼回事
謝謝大大的回答!

0
japhenchen
iT邦超人 1 級 ‧ 2021-06-30 16:29:16

在$.ajax裡加

cache: false,
contentType: false,
processData: false,

或是直接用$.post,取代底層的$.ajax
https://api.jquery.com/jquery.post/

我試過您提供的方法
結果跟之前一樣

$.post("delete.php", { filename:filename})
    .done(function (data) {
    console.log( data ); 
    },"json");

我要發表回答

立即登入回答