javascript - AJAX call sending JSON data -


i working on chat system refreshes automatically using ajax. first using jquery $.post function, since wanted return json data php script, want use $.ajax function. script worked using $.post function, can not return json. relevant code: javascript:

$.ajax({     url: "pages/loadmessage.php",     type: "post",     data: {"c": geturlparameter("c"), "t": messagetime},     datatype: "json",     success: function(pdata){         console.log(pdata);     },     error: function(xhr, status, error) {         alert(error + status);     } }); 

php code:

<?php require_once("../init.php"); header('content-type: application/json'); if (input::exists() && input::get("c") && input::get("t")) {     $chat = new chat($user->data()->id, input::get("c"));     $messages = $chat->getnewmessages(input::get("t"), $user->data()->id);     if ($messages) {         $result = array(             'topic' => $chat->gettopic(),             'messages' => array()         );         foreach($messages $m) {             array_push($result['messages'], array('source' => 'mine', 'text' => $m->text));         }         echo json_encode("string!!!");     } } else {     echo json_encode("string" . input::get("c") . input::get("t") . input::exists()); } ?> 

i tried set contenttype of ajax call "application/json" , convert data json using json.stringify, no input data gets php script. code works if 1 parameter (data: {"c": geturlparameter("c")}) sent php script... searched stackoverflow, not find solution...

thanks

json example:

index.html

<script type="text/javascript">      $.ajax({         url: "out.php",         type: "post",         data: {"param1": "test 1", "param2": "test2"},         datatype: "json",         success: function(data){             alert("param1:"+data.param1+" | param2:"+data.param2);         },         error: function(xhr, status, error) {             alert(error + status);         }     }); </script> 

out.php

<?php      if(isset($_post["param1"])){ $param1 = $_post["param1"];}     if(isset($_post["param2"])){ $param2 = $_post["param2"];}      $out = array("param1"=>$param1,"param2"=>$param2);      echo(json_encode($out)); ?> 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -