第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Ajax 從不成功:使用 xhrFields 時(shí)

Ajax 從不成功:使用 xhrFields 時(shí)

PHP
拉丁的傳說 2021-12-03 16:00:16
我無法success在我的 ajax 請(qǐng)求中觸發(fā)調(diào)用。我知道通信工作正常,但是我的PHP腳本中的最后一個(gè)調(diào)用return json_encode($array);將觸發(fā),就好像它是 onprogress 對(duì)象的一部分一樣。我想“中斷” onprogress 調(diào)用并return json_encode在 PHP 腳本終止時(shí)通過發(fā)送的最后一個(gè)數(shù)據(jù)運(yùn)行成功函數(shù)......這是我的 AJAX 調(diào)用:$( document ).ready(function(e) {    var jsonResponse = '', lastResponseLen = false;       $("#btn_search").click(function(e){      var firstname = document.getElementById('firstname').value;      var lastname = document.getElementById('lastname').value;        $.ajax({          type: "POST",          url: 'search.php',          data: $('#search_fields').serialize(),          dataType: "json",          xhrFields: {                onprogress: function(e) {                    var thisResponse, response = e.currentTarget.response;                    if(lastResponseLen === false) {                        thisResponse = response;                        lastResponseLen = response.length;                    } else {                        thisResponse = response.substring(lastResponseLen);                        lastResponseLen = response.length;                    }                    jsonResponse = JSON.parse(thisResponse);                    document.getElementById('progress').innerHTML = 'Progress: '+jsonResponse.msg;                }            },            success: function(data) {                console.log('done!');                document.getElementById('progress').innerHTML = 'Complete!';                document.getElementById('results').innerHTML = data;            }        });        e.preventDefault();    });});這是基本的 PHP 服務(wù)器腳本:<?phpfunction progress_msg($progress, $message){  echo json_encode(array('progress' => $progress, 'msg' => $message));  flush();  ob_flush();}$array = array('msg' => 'hello world');$count = 0;while($count < 100){     progress_message($count, "working....");     $count += 10;     sleep(2);}return json_encode($array);?>
查看完整描述

1 回答

?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊

我讓你的代碼正常工作,有 2 個(gè)錯(cuò)誤。首先,在你的 while 循環(huán)中,你的函數(shù)名不正確,試試這個(gè):


progress_msg($count, "working... ." . $count . "%");

其次,最后一行不輸出任何內(nèi)容,因此從技術(shù)上講,您不會(huì)得到“成功”的 json 返回。將服務(wù)器腳本的最后一行更改為:


return json_encode($array);

到:


echo json_encode($array);

更新:帶有hacky解決方案的完整工作代碼:


阿賈克斯:


$( document ).ready(function(e) {

    var jsonResponse = '', lastResponseLen = false;   

    $("#btn_search").click(function(e){

      var firstname = document.getElementById('firstname').value;

      var lastname = document.getElementById('lastname').value;

        $.ajax({

          type: "POST",

          url: 'search.php',

          data: $('#search_fields').serialize(),

          xhrFields: {

                onprogress: function(e) {

                    var thisResponse, response = e.currentTarget.response;

                    if(lastResponseLen === false) {

                        thisResponse = response;

                        lastResponseLen = response.length;

                    } else {

                        thisResponse = response.substring(lastResponseLen);

                        lastResponseLen = response.length;

                    }


                    jsonResponse = JSON.parse(thisResponse);

                    document.getElementById('progress').innerHTML = 'Progress: '+jsonResponse.msg;

                }

            },

            success: function(data) {

                console.log('done!');

                dataObjects = data.split("{");

                finalResult = "{" + dataObjects[dataObjects.length - 1];

                jsonResponse = JSON.parse(finalResult);

                document.getElementById('progress').innerHTML = 'Complete!';

                document.getElementById('results').innerHTML = jsonResponse.msg;

            }

        });

        e.preventDefault();

    });

搜索.php:


<?php

function progress_msg($progress, $message){

  echo json_encode(array('progress' => $progress, 'msg' => $message));

  flush();

  ob_flush();

}

$array = array('msg' => 'hello world');

$count = 0;

while($count <= 100){

     progress_msg($count, "working... " . $count . "%");

     $count += 10;

     sleep(1);

}

ob_flush();

flush();

ob_end_clean();

echo json_encode($array);

?>

ajax 調(diào)用的“成功”方法的問題在于它無法將返回的數(shù)據(jù)解釋為 JSON,因?yàn)橥暾姆祷厥牵?/p>


{"progress":0,"msg":"working... 0%"}{"progress":10,"msg":"working... 10%"}{"progress":20,"msg":"working... 20%"}{"progress":30,"msg":"working... 30%"}{"progress":40,"msg":"working... 40%"}{"progress":50,"msg":"working... 50%"}{"progress":60,"msg":"working... 60%"}{"progress":70,"msg":"working... 70%"}{"progress":80,"msg":"working... 80%"}{"progress":90,"msg":"working... 90%"}{"progress":100,"msg":"working... 100%"}{"msg":"hello world"}

這不是一個(gè)有效的 JSON 對(duì)象,而是一個(gè)接一個(gè)的多個(gè) JSON 對(duì)象。


我嘗試使用 刪除所有以前的輸出ob_end_clean();,但由于某種原因我無法弄清楚,它在我的設(shè)置中不起作用。因此,相反,我想出的 hacky 解決方案是不將返回視為 JSON(通過dataType從 AJAX 調(diào)用中刪除參數(shù)),并簡(jiǎn)單地用字符串操作拆分出最終的 Json 元素......


必須有一個(gè)更簡(jiǎn)單的解決方案,但如果不使用 XHR 和 Ajax 的第三方 jQuery 庫(kù),我找不到任何解決方案。


查看完整回答
反對(duì) 回復(fù) 2021-12-03
  • 1 回答
  • 0 關(guān)注
  • 587 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)