iT邦幫忙

0

AJAX抓取問題

  • 分享至 

  • xImage

各位大大好:
目前頁面想要用ajax做資料換頁及頁籤切換的function,但目前寫出來要到下頁時資料無法顯示,謝謝!

JS程式碼如下:

if(method == "littlegreenman/list"){
		var supplyCatesId		= getDataValue("supplyCatesId");
		var keyword				= decodeUrlStr(getDataValue("keyword"));	
		var dataIndex			= 0;
		var dataCnt				= 20;

		$("#tran-back-btn").attr("href",referrer);
		
		if(getDataValue("supplyCatesId") != null){
			supplyCatesId = getDataValue("supplyCatesId");
		}
		if(getDataValue("keyword") != null){
			var eventLabel		= keyword;
			keyword				= decodeUrlStr(getDataValue("keyword"));
		}
		
		var total 		= $("#page-zone").data("total");
		var totalPage 	= Math.ceil(total/dataCnt);
		var nowPage 	= 1;
		var pageArray 	= pageSet(nowPage,totalPage);
		var beginPage 	= pageArray[0];
		var endPage 	= pageArray[1];
		pageZoneSet(nowPage,beginPage,endPage,totalPage,total,dataCnt,"");
		$("#page-zone").find(".page-btn").unbind("click");
		$("#page-zone").find(".page-btn").bind("click",function(){
			var selectPageIndex = parseInt($(this).attr("alt"));
			selectGreenmanByCates(supplyCatesId,keyword,selectPageIndex,dataCnt);
		});
		$("#page-next").unbind("click");
		$("#page-next").bind("click",function(){
			var nextPageIndex = (nowPage)*dataCnt;
			selectGreenmanByCates(supplyCatesId,keyword,nextPageIndex,dataCnt);
		});		
	}
function transLittleGreenmanListPage(){
	var supplyCatesId 	= decodeUrlStr(getDataValue("supplyCatesId"));
	var keyword = $("#search-text").val();
	saveGreenmanUrl(supplyCatesId,keyword);
	selectGreenmanByCates(supplyCatesId,keyword);
}
function saveGreenmanUrl(supplyCatesId,keyword){
	var data_json 	= {};
	var appendData 	= "";
	var saveLink 	= relativeNewPath+"littlegreenman/list";
	supplyCatesId 	= decodeUrlStr(getDataValue("supplyCatesId"));
	keyword			= decodeUrlStr(keyword);
	if(supplyCatesId != null && supplyCatesId != ""){
		if(appendData == ""){
			appendData = appendData+"?";
		}else{
			appendData = appendData+"&";
		}
		appendData = appendData+"supplyCatesId="+supplyCatesId;
		data_json['supplyCatesId'] = supplyCatesId;
	}
	if(keyword != null && keyword != ""){
		if(appendData == ""){
			appendData = appendData+"?";
		}else{
			appendData = appendData+"&";
		}
		appendData = appendData+"keyword="+keyword;
		data_json['keyword'] = keyword;
	}
		
	saveLink = saveLink+appendData;
	save_url(data_json, document.title, saveLink);
}
function selectGreenmanByCates(supplyCatesId,keyword,dataIndex,dataCnt){
	$("#search-list").html("");
	$("#page-zone").html("");	
	$.ajax({
		url : relativeNewPath+"ajax/selectGreenmanByCates",
		type : "GET",
		dataType : "json",
		data:{
			supplyCatesId : supplyCatesId, 
			keyword : keyword,
			dataIndex : dataIndex,
			dataCnt : dataCnt
		},

		error : function(data) {
			$("#loading").hide();
			console.log("selectGreenmanByCates","error");
		},
		success : function(data) {
			console.log("selectGreenmanByCates",data.status);
			$("#loading").hide();
			if(data.status!="selectGreenmanByCatesSuccess"){
				$(".data-total").html(0);
				$("#page-zone").attr("data-total",0);
				$("#page-zone").data("total",0);
				$("#search-list").html("<tr><td>查無資訊</td></tr>");
			}else{
				var total = data.total;
				$(".data-total").html(total);
				$("#page-zone").attr("data-total",total);
				$("#page-zone").data("total",total);
				var totalPage 		= Math.ceil(total/dataCnt);
				var nowPage 	= (dataIndex/dataCnt)+1;
				var pageArray 	= pageSet(nowPage,totalPage);
				var beginPage 	= pageArray[0];
				var endPage 	= pageArray[1];
			
				pageZoneSet(nowPage,beginPage,endPage,totalPage,total,dataCnt,"");
				$("#page-prev").unbind("click");
				$("#page-prev").bind("click",function(){
					var prevPageIndex = (nowPage-2)*dataCnt;
					selectGreenmanByCates(supplyCatesId,keyword,prevPageIndex,dataCnt);
				});
				$("#page-zone").find(".page-btn").unbind("click");
				$("#page-zone").find(".page-btn").bind("click",function(){
					var selectPageIndex = parseInt($(this).attr("alt"));
					selectGreenmanByCates(supplyCatesId,keyword,selectPageIndex,dataCnt);
				});
				$("#page-next").unbind("click");
				$("#page-next").bind("click",function(){
					var nextPageIndex = (nowPage)*dataCnt;
					selectGreenmanByCates(supplyCatesId,keyword,nextPageIndex,dataCnt);
				});	

				for(var g = 0 ;g < data.greenman.length ; g++){
					var nutritionid     = data.greenman[g].nutritionid;
					var genericName 	= decodeUrlStr(data.greenman[g].nutrition);
					var nutrition     	= data.greenman[g].nutrition;
					var greenmanLink 	= relativeNewPath+"littlegreenman/information?nutritionid="+nutritionid;

					$("#search-list").append(
					"<tr>" + "<td class=\"ellipsis\" style=\"width:25%; \"><a target=\"_blank\" href=\""+greenmanLink+"\">"+genericName+"</a></td>" + "</tr>"
					);

				}
			}
		}
	});
}

PHP程式碼如下:

<?php
/*******$_GET supplyCatesId and keyword********/	
			if(isset($_GET["keyword"])){
				$searchType="keyword";
				$searchval=$_GET["keyword"];
				$selectType_n=$searchval;
			}
			elseif($_GET["supplyCatesId"]!=""){
				$searchType="benefitIds";
				$searchval=$_GET["supplyCatesId"];	
				$greenmanListResult = selectGreenmanBenefitList($requestLanguage,$databaseIP,$database,$databasePort);					
				while(list($k,$v)=each($greenmanListResult["SupplyCates"])){
					$shortName[$v["supplyCatesId"]]=$v["supplyCatesShort"];
					$CateName[$v["supplyCatesId"]]=$v["supplyCatesShort"];
				}
				$searchcates=explode(",",$searchval);				
				updateGreenmanBenefitClicks($searchcates,$requestLanguage,$databaseIP,$database,$databasePort);  //----增加click數			
				$selectType_n=array();
				foreach($searchcates as $scv){
					if(!in_array($shortName[$scv],$selectType_n)) $selectType_n[]=$shortName[$scv];
				}
				$selectType_n=implode(",",$selectType_n);
			}
			$dataIndex=0;
			$dataCnt=20;
			$selectResult=selectGreenmanByCates($searchType,$searchval,$dataIndex,$dataCnt,$requestLanguage,$databaseIP,$database,$databasePort);
			$dataarr=array();
			if($selectResult!="selectGreenmanByCatesFalse"){
				$totalRows=$selectResult["total"];								
				foreach($selectResult["greenman"] as $val){
					$dataarr[]= "
<td class=\"ellipsis\" style=\"width:25%; \">
<a target=\"_blank\" href=\"".$relativeNewPath."littlegreenman/information?nutritionid=".$val["nutritionid"]."\">".$val["nutrition"]."</a>
</td>	";
				}
			}	
?>
看更多先前的討論...收起先前的討論...
qpalzm iT邦新手 1 級 ‧ 2020-10-29 15:24:24 檢舉
前端:dataType : "json" 表示 後端要傳JSON格式回去才能抓到, PHP 要傳值 echo 後面接要傳的值(json)。建議var 可以改為let 會比較好喔~
先用F12 開發者模式看 network 觸發 ajax 後回傳的資料
hi16p iT邦新手 5 級 ‧ 2020-10-30 11:12:10 檢舉
AJAX正常回傳.但頁面沒SHOW資料 . 是因為少了ID秀資料的function ???
你在PHP 檔案下面試著用 print_r 或者是 var_dump 把你要的資料先印出來看看嗎 ?
hi16p iT邦新手 5 級 ‧ 2020-11-03 13:21:04 檢舉
感謝各位大大的幫忙 . 現在資料出現了但還有兩個問題無解 :
1.搜尋後網址有帶參數卻沒顯示搜尋結果,需按F5才會產生結果
2.頁籤第1頁切換第2頁後沒有篩選資料而是全數匯入

新手問題多. 還請各位多包涵~
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1

請善用f12大法。請善用f12大法。請善用f12大法。(很重要,講三次)

你就會發現你的問題在哪。

其實你的問題看來你並未正確的傳回json格式的資料。
理論上你的ajax因該是出錯的才對。

先確定好你的回傳值。

我要發表回答

立即登入回答