我有一個由 php 和 javascript 撰寫的專案......我有一個通過 Php 代碼從相機呼叫 API。我使用 (file_get_contents) 函式和 Post 請求:此代碼:
$value = $_POST['TakeNewPic'];
function Takepic(){
$Parametrs = $value;
$opts = array(
"user" => "THETAYN10113693",
"password" => "10113693",
"name" => $Parametrs
);
$post = json_encode($opts);
$context = stream_context_create(array(
'http' => array(
'method' => "POST",
'timeout' => 60,
'content' => $post,
'header' => "Content-Type : application/json"
)
));
$url = 'http://192.168.1.1/osc/commands/execute';//.$https_server;
$result = file_get_contents($url,false, $context);
echo json_encode($result);
//}
}
此代碼正在運行并且相機拍照,但問題是我總是收到此通知:
注意:file_get_contents(): Content-type 未指定假設 application/x-www-form-urlencoded 在 C:\xampp7.3\htdocs\CameraTest\function.php 第 63 行
我通過來自 Java 腳本檔案的 Ajax 呼叫將值發送到 php 檔案,這是 Java 腳本檔案中的代碼:
function getCamInfo(value)
{
var body = "TakeNewPic=" value ;
var req = new XMLHttpRequest();
req.onreadystatechange = function ()
{
if(this.readyState === 4 && this.status === 200){
var res = document.getElementById("parg");
res.innerHTML =this.responseText;
}
}
req.open (
"POST",
"post_req.php",
true,
);
req.setRequestHeader(
"content-type",
"application/x-www-form-urlencoded"
);
req.send(body);
}
document.getElementById("butt").onclick = function ()
{
getCamInfo("camera.takePicture");
//console.log(AllInfo);
}
請我不要再得到這個通知我希望你的幫助和非常感謝
uj5u.com熱心網友回復:
查看實作(例如PHP 7.4.0,PHP 8.1.0),如果標題名稱和冒號之間有空格,則看起來不會檢測到標題。這可能是一個錯誤,但很容易解決 - 而不是:
'header' => "Content-Type : application/json"
嘗試:
'header' => "Content-Type: application/json"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/386021.html
標籤:javascript php 阿贾克斯 要求
