2015-07-12 85 views
0

請問我在Apps腳本中使用POST方法時出現了什麼問題?Google Apps腳本中的POST方法

function myFunctionpost() { 
    var url = "mydomain.com/2spreadsheet.php"; 
    var data = { "testpost" : " Text text text" }; 
    var payload = JSON.stringify(data); 

var headers = { "Accept":"application/json", 
       "contentType" : "application/json", 
       "Authorization":"Basic _authcode_" 
      }; 

var options = { "method":"POST", 
      "contentType" : "application/json", 
      "headers": headers, 
       "payload" : payload 
      }; 

    var response = UrlFetchApp.fetch(url ,options); 
    Logger.log(response); 
} 

myFunctionpost()在應用程序腳本即將發佈的JSON數據: 「testpost」 的網址。

<?php 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    echo "Server is requested"; 

    if(isset($_POST["testpost"])){ 
     echo "Post something here"; 
     echo $_POST["testpost"]; 
    } 
} 

而且表明我要發佈我張貼的數據,但它不工作的代碼(2spreadsheet.php)。

的洛輸出:

[15-07-12 14:06:00:205 HKT]

服務器請求

誰能告訴我是什麼問題?

回答

0

您不必指定內容類型,可以省略JSON.Stringify。您的對象數據已經是一個JavaScript對象。它將被解釋爲一個HTML表單。

var url = "mydomain.com/2spreadsheet.php"; 
var data = { "testpost" : " Text text text" }; 

var options = { "method":"POST", 
       "payload" : data 
}; 

var response = UrlFetchApp.fetch(url ,options); 
Logger.log(response); 

內容類型將默認爲application/x-www-form-urlencoded。你可以用$ _SERVER [「CONTENT_TYPE」]檢查它。