英文:
Warning error: Trying to access array offset on value of type null php
问题
这是 JavaScript 代码中的问题:
let digit_key = document.getElementById("input_compare" + numInput).value;
应该用正常的引号 "
替换 "
,所以应该是:
let digit_key = document.getElementById("input_compare" + numInput).value;
这个问题会导致 JavaScript 代码中的字符串不正确,可能会影响到 API 请求。请更正这个错误,然后再测试一次您的代码。
希望这可以帮助您解决问题。如果还有其他问题,请告诉我。
英文:
From javscript code I am comsuming an api rest using a fectch but I receive this error:
Warning error: Trying to access array offset on value of type null php
This api has been made in php. If I call this api from soap ui work fine but when I call this api from javscript code receive the error:
This is the javsacript code:
function compareValueKey(numInput) {
let digit_key = document.getElementById("input_compare" + numInput).value;
let configFetch = {
method: "POST",
body: "digit_key=" + digit_key + "&numInput=" + numInput,
headers: {'Content-Type': 'application/x-www-form-urleancoded'}
};
let promesa = fetch("compareValueKey.php", configFetch);
promesa.then(function(response){
if(response.ok){
console.log("Respuesta OK");
}
response.json().then(
function(objectoJSON){
let valido = objectoJSON.valido;
let numInput = objectoJSON.n;
document.getElementById("input_compare"+ numInput).nextElementSibling.innerHTML = valido;
});
}).catch(function (error){
console.log('Error con la peticion' + error.message);
});
}
This is php code:
<?php
session_start();
$key =1111;
$data = json_decode(file_get_contents('php://input'), true);
$digit_key = $data['digit_key'];
$numInput = $data['numInput'];
if($digit_key == substr($key, $numInput - 1, 1)){
echo json_encode(array("valido" => "ok", "n" => $numInput));
}else{
echo json_encode(array("valido" => "error", "n" => $numInput));
}
and this is the html code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script defer src="actividad06.js" pe="text/javascript"></script>
<title>Actividad 6</title>
</head>
<body class="p-5">
<div id="checkCombination">
<input type="number" id="input_compare1" onkeyup="compareValueKey(1)"><span class="msg"></span>
<input type="number" id="input_compare2" onkeyup="compareValueKey(2)"><span class="msg"></span>
<input type="number" id="input_compare3" onkeyup="compareValueKey(3)"><span class="msg"></span>
<input type="number" id="input_compare4" onkeyup="compareValueKey(4)"><span class="msg"></span>
</div>
</body>
</html>
I think that there is an error in javascript code.
Could you be so kind to check?
答案1
得分: 0
你的POST数据不符合有效的JSON格式,但你期望在你的PHP代码中使用它。你的POST数据是URL编码的。
以下这行代码将会给你一个有效的JSON字符串,类似于 { "digit_key" : "123", "numInput" : "456" }
body: `{ "digit_key" : "${digit_key}", "numInput" : "${numInput}" }`,
此外,你的PHP代码中存在类型错误。substr
函数期望参数1是一个字符串,但你给定的是一个整数 $key = 1111
。在较新的PHP版本中,你将会遇到问题。为了编写干净的代码,使用一个字符串而不是整数,这将在启用strict_types
的情况下正常工作:
$key = "1111";
英文:
You do not post a valid JSON format but you expect that in your PHP code. Your POST data is URL encoded instead.
The following line will give you a valid JSON string like { "digit_key" : "123", "numInput" : "456" }
body: `{ "digit_key" : "${digit_key}", "numInput" : "${numInput}" }`,
Further more there is a type error in your PHP code. The substr
function expects a string as argument 1, but an integer $key = 1111
is given. You will run into an issue in newer PHP versions. For clean code use a string instead which will work with strict_types
enabled as well:
$key = "1111";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论