2016-07-14 2 views
0

jQuery AJAXを使用して、JavaScript、PHP、およびHTMLからなる文字列をPHPから返します。jQuery Ajax複数のデータを返します

私は成功し、次のコードでこれを行うことができます。

header("Content-Type: text/html"); 
echo $content; 

$.ajax({ 
    type: 'POST', 
    url: url, 
    data: data, 
}).done(function(result) { 

}).fail(function(jqXHR, textStatus, errorThrown) { 
    console.log(jqXHR, textStatus, errorThrown); 
}); 

私が今持っている問題は、私も、この文字列に沿って他のいくつかの単純な値を返すようにしたいということです。

ただし、json_encodeを使用してこれらの値の配列を送信すると、文字列が破損し、正常に処理されません。

文字列(json_encodeを除く)とjson_encodeのいくつかの値を1つの値として送信するにはどうすればよいですか? (私はしないjson_encode私の文字列)

EDIT1:

return 'autoOpenPopup: '.!empty($options["autoOpenPopup"]) ? $this->int_to_bool($options["autoOpenPopup"]) : $this->int_to_bool(false) . PHP_EOL .'; 

2:

return '.!isset($options["popupInit"]) ? 

       $playerId.' = jQuery("#'.$wrapperId.'").hap(settings); 

      ':' 

       if(hasLocalStorage){ 

        if(!localStorage.getItem("hap_popup_fixed")){ 
         '.$playerId.' = jQuery("#'.$wrapperId.'").hap(settings); 
        } 
       }else{ 
        '.$playerId.' = jQuery("#'.$wrapperId.'").hap(settings); 
       } 
+0

あなたの文字列を配列に入れたら、両方とも 'json_encode'ですか? – Chay22

+0

私はjson_encodeを自分の文字列の上で試しました。それを配列に入れても何も変わっていないようです:http://pastie.org/10907465 – Toniq

+0

実際に何が壊れているかを見つけるために実際のコードを投稿するべきです。 – Chay22

答えて

3

最良の方法は、json_encodeあなたのデータにある

ここ

する問題1をフォーマットされましたと一緒に弦;

$data = array('some', 'array', 'elements'); 

$string = 'my string'; 

$data2 = array('more', 'data'); 

次に、あなたは1つの配列にそれらのすべてを兼ね備え:

$result = array(); 
$result['data1'] = $data; 
$result['string'] = $string; 
$result['data2'] = $data2; 

最後にjson_encode配列:

echo json_encode($result); 

を次にあなたはJSでの結果読み:

$.ajax({ 
    type: 'POST', 
    url: url, 
    data: data, 
}).done(function(result) { 
    var jsonResult = $.parseJSON 
    var data1 = result.data; 
    var data2 = jsonResult.data2; 
    var str = jsonResult.string; 

}).fail(function(jqXHR, textStatus, errorThrown) { 
    console.log(jqXHR, textStatus, errorThrown); 
}); 
+0

私はjson_encodeを自分の文字列の上で試しました。 (私は、PHP、HTML、JavaScriptの長いミックスのため) – Toniq

+0

あなたが見て私たちとの文字列を共有することができますか? –

+0

また、私の例に基づいてデータを読むためのコードを含めます –

0

header( 'コンテンツタイプ:アプリケーション/ json '); echo json_encode($ data、true);

あなたの$ .ajax({... dataType: 'json'});

関連する問題