我想向PHP文件发送ajax调用,其中我有Javascript代码将数据发送到SurveyJS(https://surveyjs.io/)。
使用" localhost / testSurvey / ajaxOriginator.html"调用ajaxOriginator.html成功调用receiver.php并在receiver.php中运行JavaScript但不向SurveyJS发送请求。
如果我直接使用" localhost / testSurvey / receiver.php"来调用receiver.php,它会成功将请求发送给SurveyJS,我可以看到收到的数据。
这是ajaxOriginator.html
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Ajax originator</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$.get('receiver.php', { "name": "Julia", "car": "Kia" });
</script>
</head>
这是receiver.php
<?php
$name = $_GET["name"];
$car = $_GET["car"];
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://surveyjs.azureedge.net/1.0.16/survey.jquery.min.js"></script>
<script>
var json = {};
window.survey = new Survey.Model(json);
//To test if ajax call works and it does
var values = ["<?php echo $name; ?>", "<?php echo $car; ?>"];
//To send data directly to SurveyJS
values = ["John", "somecar"];
var deepSurveyCopy = $.extend(true, {}, survey);
console.log(deepSurveyCopy);
deepSurveyCopy.data.name = values[0];
deepSurveyCopy.data.car = values[1];
deepSurveyCopy.sendResult(my access key to surveyJS);
</script>
<?php
echo "Welcome ". $name. "<br />";
echo "You want ". $car;
?>
以下是Chrome和SurveyJS的屏幕截图:
我不是非常精通PHP,并且非常感谢您帮助理解所有这些工作(而不是工作)。
基本上ajaxOriginator.html是模拟一个插件,它将把数据发送到receiver.php,我试图用SurveyJS接受的格式包装收到的数据并重新发送到SurveyJS。
非常感谢任何帮助。谢谢!