2015-09-10 58 views
0

你好朋友我有一個php代碼的問題,我需要使用AJAX響應。「include a php file with html code」for working with ajax response

PHP代碼不工作PHP Ajax響應:

if($_GET){ 
    $data['form'] =  $this -> AJAXFORM  -> GetHTMLAjaxForm(); 
    $return = json_encode(array('form'=>$data['form'])); 
    echo $return; 
} 

GetHTMLAjaxForm()//功能

function GetHTMLAjaxForm(){ 
    $htmlform  =  include('adduser.php'); 
    return $htmlform; 
} 

文件adduser.php有需要被用作enterely HTML代碼響應ajax回調,plzz檢查它,並說我是否有任何錯誤,因爲當我使用它得到這個錯誤:

Ajax, php respond array data error SyntaxError: JSON.parse: unexpected character at line 4 column 7 

更新 這一工作的一部分,現在已成了一個新的問題如下:

https://stackoverflow.com/questions/32516272/html-inserted-with-ajax-not-work-with-other-js-script

+0

似乎您試圖解析響應爲JSON,這是不行的,如果它是HTML 。 – GolezTrol

+0

但是一定有辦法做到這一點? –

+0

解析json是必需的,因爲它將是一個數組。 –

回答

0

,最終得到運行這個想法是要仔細GET請求由阿賈克斯

function GetForms(id){ 
    $.ajax({ 
     type: "GET", 
     dataType: "json", 
     url:  "index.php", 
     data: {idfield:id}, 
     error: function(xhr,status,error){alert(error);}, 
     success: function(data) { 
      $("#areatitle").html(data.title); 
     } 
     }); 
    $.ajax({ 
     type: "GET", 
     url:  "index.php", 
     data: {idform:id}, 
     error: function(xhr,status,error){alert(error);}, 
     success: function(response) { 
      $("#formarea").html(response); 
     } 
    }); 
} 

和PHP文件:

if($_GET){ 
    if(@$_GET['idfield']){ 
     $return = json_encode(array('title'=>'titulo nuevo')); 
     echo $return; 
    } 
    if(@$_GET['idform']){ 
     $data =  $this -> AJAXFORM  -> GetHTMLAjaxForm(); 
     echo $data; 
    } 
} 

實際上我想要的是簡化向服務器發送請求的數量並控制這一點非常重要。

如果有人有更好的主意favort在這篇文章中溝通。

這一工作的一部分,現在已成了下面列出的新問題:

https://stackoverflow.com/questions/32516272/html-inserted-with-ajax-not-work-with-other-js-script

+1

不要使用'@',因爲你會忽略錯誤。大聲笑 – aldrin27

+0

需要正常工作,因爲每個.ajax()運行不同的請求,需要採取相同的步驟.. –