php-JQueryの問題getJSONがコントローラー(Codeigniter)からデータを取得する
JQuery、Ajax、およびPHPコードに問題があります。
ユーザーからのすべての投稿を含むページがあります。
次に、このページで、Ajaxを使用してその投稿の最新のコメントをロードします。
ただし、何も表示されません。
これが私がJQuery関数を呼び出したPHPコードです:
<? foreach($post_query as $post_row){ ?>
<li class="post_list">
<?php echo $post_row['text'];?>
<br/>
Latest Comment:
<br/>
<script type="text/javascript">
var post_id = $('#post_id_<?php echo $post_row['id'];?>').val();
document.write(post_id);//just to check that post_id value is not undefined
$(function(){
$.getJSON("<?php echo base_url();?>postC/latest_comment", {post_id: post_id},
function(res){
$("#result").prepend(res.text);
});
});
</script>
<? } ?>
これは、リクエストを処理する私のPHP Code Igniterコントローラーです:
class postC extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('models_facade');
$this->load->Database();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('tank_auth');
$this->load->library('user_session_lib');
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
}
function latest_comment(){
$post_id = $this->checkValues($_GET['post_id']);
$latestcomment = $this->models_facade->getLatestCommentForPost($post_id);
return json_encode($latestcomment);
}
}
関数を手動で呼び出し、ajaxを使用せずに最新のコメントを表示することをテストしましたが、機能しました。つまり、これは、mysqlから結果を取得するモデルのバックエンド関数に問題がないことを意味します。
私もやろうとしました:
echo json_encode($latestcomment);
関数を手動で呼び出そうとすると、データがJSON形式であることがわかりました。
つまり、これは、ajax呼び出しに問題があり、コントローラーからデータを取得しなかったことを意味します。 .getJSONにはGETを使用し、.post()と.ajax()にはPOSTを使用しました。
jQuery Ajax呼び出しのすべてのバージョン(.getJSON()、. post()、および.ajax())を試しましたが、どれも機能しませんでした。 他のすべてのajax関数が正しく機能する可能性があるため、対応するJqueryライブラリが正しく読み込まれていると確信しています。
エラーが発生する可能性のある手がかりはありますか?これを丸1日デバッグしましたが、結果が得られませんでした。
ありがとう
答え :
解決策:
これがjQueryCodeigniter呼び出しの処理方法であり、機能します。
jQueryでこれを試してください:
$(function(){
$.post("<?php echo base_url();?>postC/latest_comment", {post_id: post_id},
function(data){
$("#result").prepend(data.html);
},'json');
});
PHP:
function latest_comment(){
$post_id = $this->checkValues($_GET['post_id']);
$jsonData['html'] = $this->models_facade->getLatestCommentForPost($post_id);
echo json_encode($jsonData);
}
これにより、返されるJSONオブジェクトにさらに多くを入れることができます。私が使用したいトリックの1つは、「failed」と「msg」の値を配列に追加することです。したがって、ユーザーに通知する必要がある場合は、失敗したことと、おそらくその理由をユーザーに伝えることができます。
お役に立てば幸いです
//ランス
答え :
解決策:
設定ファイルでクエリ文字列を有効にしていないと思いますが、これでうまくいく可能性があります。
javascript:
$(function(){
$.getJSON("<?php echo base_url();?>postC/latest_comment/" + post_id, function(res){
$("#result").prepend(res.text);
});
});
およびphpコントローラーの場合:
function latest_comment($post_id)
{
$post_id = $this->checkValues($post_id);
$latestcomment = $this->models_facade->getLatestCommentForPost($post_id);
// Better practise to put json header
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
return json_encode($latestcomment);
}
答え :
解決策:
これがjQueryCodeigniter呼び出しの処理方法であり、機能します。
$(function(){
$.post("<?php echo base_url();?>postC/latest_comment", {post_id: post_id},
function(data){
$("#result").prepend(data.html);
},'json');
});
PHP:
function latest_comment(){
$post_id = $this->checkValues(**$_GET['post_id']**);
$jsonData['html'] = $this->models_facade->getLatestCommentForPost($post_id);
echo json_encode($jsonData);
}
これにより、返されるJSONオブジェクトにさらに多くを入れることができます。私が使用したいトリックの1つは、「failed」と「msg」の値を配列に追加することです。したがって、ユーザーに通知する必要がある場合は、失敗したことと、おそらくその理由をユーザーに伝えることができます。
お役に立てば幸いです
この回答では $_POST['post_id']
は、私の場合、 $_GET['post_id']
答え :
解決策:
私の仲間の友人は同じような状況にありました。 return encode jsonを使用する代わりに、結果をエコーアウトしてみてください。基本的に必要なのは、それを解析する準備ができたjsonデータファイルとjsスクリプトです。あなたの場合、結果はただ浮かんでいて、ビューにエコーされるのを待っています。お役に立てば幸いです。
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。