Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用fetch将数据发布到php_Javascript_Php_Fetch_Nativescript - Fatal编程技术网

Javascript 如何使用fetch将数据发布到php

Javascript 如何使用fetch将数据发布到php,javascript,php,fetch,nativescript,Javascript,Php,Fetch,Nativescript,我正在尝试使用我的brwser上的链接为我的nativescript应用程序创建一个注释函数,该功能运行良好。但在我的应用程序中使用fetch API时,它返回错误函数(错误消息是位置0处的意外标记array(“text”=>$e->getMessage());回显json_encode($output);。永远不要手动生成JSON。@CherryDT错误消息是JSON中位置0处的意外标记,因此存在HTML而不是JSON。请使用response.text()而不是response.JSON(),

我正在尝试使用我的brwser上的链接为我的nativescript应用程序创建一个注释函数,该功能运行良好。但在我的应用程序中使用fetch API时,它返回错误函数(错误消息是位置0处的意外标记<在JSON中)

我的php是

<?php 

include 'config.php';
$number = $_GET['number'];
$comment = $_GET['comment'];
$email = $_GET['email'];
try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    $sql = "SELECT name FROM searchResults WHERE email='$email'";
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare($sql);  
    //$stmt->bindParam(":email", $_GET['email']);
    $stmt->execute();
    $employees = $stmt->fetch(PDO::FETCH_ASSOC);
    $employees1 = $stmt->fetchAll(PDO::FETCH_OBJ);
    $name = $employees['name'];
    $sql1 = "INSERT INTO comments (number, comment, user, date) VALUES ('$number', '$comment', '$name', '02/04/2020')";
    $sth = $dbh->query($sql1);
    $sth->execute();
    $dbh = null; 
    echo '{"itemss":'. json_encode($employees1) .'}';
    //$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

?>

JSON中位置0处的错误
意外标记<很可能意味着您得到的是HTML而不是JSON。(想象一下将
..
解析为JSON-然后第一个字符是
好的,哪个错误?首先检查
err
的内容。你得到了什么确切的错误?你检查了吗?P.S.
echo'{“itemss”:.JSON_encode($employees1)。'};
是生成JSON的坏方法。相反,写
$output=array(“itemss”=>$employees1);回显json_encode($output);
。回显异常消息时的原理相同:
$output=array(“error”=>array(“text”=>$e->getMessage());回显json_encode($output);
。永远不要手动生成JSON。@CherryDT错误消息是JSON中位置0处的意外标记,因此存在HTML而不是JSON。请使用
response.text()
而不是
response.JSON()
,然后打印您得到的
res
,以便在将实际内容解析为JSON失败之前查看它们。(理想情况下,您可以使用调试代理来查看实际发生的情况。)哦,我发现了其他问题-写了一个答案,天哪!非常感谢。我看了那行大约一个小时,没有注意到该符号丢失@CherrydYou are welcome:)提示:查看请求可能会有所帮助。请检查,然后下次您可以查看“网络”选项卡以查看请求和响应,您会注意到a)HTML返回及其内容,b)错误的请求数据您的建议说URL未定义,请注意,您是对的,NativeScript没有
URL
对象。我正在修改我的答案。更新:)很抱歉。
<?php 

include 'config.php';
$number = $_GET['number'];
$comment = $_GET['comment'];
$email = $_GET['email'];
try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    $sql = "SELECT name FROM searchResults WHERE email='$email'";
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare($sql);  
    //$stmt->bindParam(":email", $_GET['email']);
    $stmt->execute();
    $employees = $stmt->fetch(PDO::FETCH_ASSOC);
    $employees1 = $stmt->fetchAll(PDO::FETCH_OBJ);
    $name = $employees['name'];
    $sql1 = "INSERT INTO comments (number, comment, user, date) VALUES ('$number', '$comment', '$name', '02/04/2020')";
    $sth = $dbh->query($sql1);
    $sth->execute();
    $dbh = null; 
    echo '{"itemss":'. json_encode($employees1) .'}';
    //$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

?>