2017-03-07 36 views
0

我已經使用郵遞員發送json數據到指定的url.I得到reposured響應是郵政中的json。我想發送這個json到其他文件所以我用ajax post method.Ajax post方法沒有被調用。我的函數在這裏沒有觸發成功或錯誤。Ajax成功和錯誤函數沒有在jquery中正確調用

<?php 
// get the HTTP method, path and body of the request 
$method = $_SERVER['REQUEST_METHOD']; 
$request = explode('/', trim($_SERVER['PATH_INFO'],'/')); 
$input = json_decode(file_get_contents('php://input'),true); 
$value1=json_encode($input); 
//echo print_r($value1,1); 
?> 
<script type="text/javascript"> 
    $(document).ready(function() { 
    // alert("hello"); 
     var jsondata = <?php echo $value1 ?>; 
     $.ajax({ 
     url: "/restapi/subscribe.php", 
     type: "POST", 
     data: { jsondata:jsondata }, 
     dataType: "json", 
     Content-Type: "application/json", 
     success: function() { 
      alert("data sent!"); 
     }, 
     error: function() { 
      alert("There was an error. Try again please!"); 
     } 
     }); 
     return false; 
    }); 
</script> 

我發送任何JSON數據作爲input.Output還JSON數據應在subscribe.php.In subscribe.php張貼我寫

<?php 
$json=$_POST['jsondata']; 
echo $json; 
?> 
+0

也許這是因爲錯誤。你拼錯'contentType'。 'Content-Type'無效js – disstruct

回答

4

來取代它與下面的代碼嘗試

<script type="text/javascript"> 
    $(document).ready(function() { 
    // alert("hello"); 
     var jsondata = <?php echo $value1 ?>; 
     $.ajax({ 
     url: "/restapi/subscribe.php", 
     type: "POST", 
     data: { jsondata:jsondata }, 
     dataType: "json", 
     contentType: "application/json; charset=utf-8", 
     success: function() { 
      alert("data sent!"); 
     }, 
     error: function() { 
      alert("There was an error. Try again please!"); 
     } 
     }); 
     return false; 
    }); 
</script> 

,讓我知道,如果它的工作

+0

謝謝你對我有用 – Manasa

0

我認爲這個問題是與Content-Type 嘗試用contentType:"application/json"

+1

echo json_encode($ json); –

1

您應該刪除內容類型:從你的JS代碼「應用/ JSON」,並在你的PHP腳本中使用下面的代碼:

header('Content-Type: application/json'); 
echo json_encode($data); 
0

嘗試更換Content-Type: "application/json"contentType: "application/json; charset=utf-8",

相關問題