【发布时间】:2019-01-25 09:08:35
【问题描述】:
我有一个 HTML 页面,我需要通过它通过 post 方法将两个变量传递到一个 PHP 页面,该页面接受这两个变量并调用 API 以从这两个变量中获取结果。 现在,我的要求是将 API 结果返回到第一页。 我无法得到它。
从 index.html,我正在调用另一个页面 -
$.ajax({
url: "data.php",
type: 'POST',
data: {
difficulty: term,
cr : country
},
success: function (res) {
console.log(res);
}
在data.php中,我通过POST方法获取参数并调用API,
$(document).ready(function(){
var data;
$.ajax({
url: "https://key***.co*/api?key="+api_key,
dataType: "json",
data: {
difficulty: <?php echo $diff; ?>,
cr : <?php echo $cr; ?>
},
success: function (res) {
data = difficulty;
}
});
}
这里的 api_key 是上面定义的。如何将数据取回 index.html?
【问题讨论】:
-
我建议使用PHP方法来访问API,但AJAX。在这种情况下,您将在
index.html的正确回调中获得最终响应。 -
“如何将数据取回 index.html?” - 你不能(你现在的做法)。发出(php)curl请求而不是data.php中的ajax;然后您可以将结果返回到 index.html
-
data.php 中的 ajax 无论如何都不会执行,因为它永远不会看到可以执行 javascript 的浏览器。 (除非你通过 phantom.js 或 jsonp 返回做一些花哨的事情)
-
这可能对你有用 - stackoverflow.com/questions/13840429/… 你正在做的是混淆你的 php 文件中的服务器端和客户端编程
-
我不明白为什么你不能直接在第一页调用 api。在索引和数据页面之间移动时您没有添加任何内容
标签: javascript php jquery json ajax