将 JS 变量推送到 PHP 时出现 404,调用 PHP 函数,将结果作为 JSON 字符串传递给 JS,并且在使用它时可能会失败

404 while pushing JS variables to PHP, calling a PHP function, getting result as a JSON string to JS and possibly failing while working with it

因此,我发现了关于我标题中所有子主题的大量 Whosebug 问题和答案。将它们链接在一起并得到 404。 我正在使用 MEAN 堆栈构建简单的 API 搜索应用程序。

这是'together.php'的回显(功能正常):

$variable1 = $_POST['JavaScriptButtonVariable1'];
$variable2 = $_POST['JavaScriptButtonVariable2'];
echo json_encode(inst_search(myfunction($variable1, $variable2)));

这是我的 JSON object:

{"Target hashtag searched":"pizza","Additional keyword searched":"italia","Number of instagram submitters this session":20,"Total number of tags submitted":242,"Score of this hashtag\/keyword pair this session":6,"Date and time (YYYY\/MM\/DD HH:MM:SS)":"2015\/07\/23 09:34:55am","0":[{"Location":null,"Tags":["bandung","jakarta","pizzaitalia","pizza"]},{"Location":{"Location":"Catania","Area":"Catania","Region":"Provincia di Catania"},"Tags":["casa","famiglia","food","sicily","pizza

我感兴趣的键是:'Number of instagram submitters this session'、'Total number of tags submitted'、'Score of this hashtag/keyword pair this session' 和 'Date and time (YYYY/MM/DD HH:MM:SS)',因此我使用 [2]、[3]、[4]和 [5](在下面的代码中可见)。每次只有一个值分配给这些键。

这是我的 'global.js' 文件中调用 php 文件的函数部分:

var parsedData = [];
var eins = $('#variable1').val();
var zwei = $('#variable2').val();
$.post
('together.php',{variable1:eins,variable2:zwei},function(omg)
    {
        var parsedData = JSON.parse(omg);
    }
);
var php_no_sub = parsedData[2];
var php_tags_sub = parsedData[3];
var php_score = parsedData[4];
var php_datetime = parsedData[5];

  var newTag = 
    {
        'searchrecords': php_no_sub.val(),
        'tagsfound': php_tags_sub.val(),
        'datetime': php_score.val(),
        'score': php_datetime.val()
    }

现在,我将代码简化为可能导致问题的部分,包括调用时的 404 错误以及我可能极度无法从 JSON 中提取的数组正确构造 JS objects .

当点击 link http://localhost... 我的 together.php 被下载,所以它肯定在正确的目录中。

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3000/together.php  

问题:为什么我的控制台调用 404?数组和 object 的代码是否正确编写?编辑:如何允许通过路由发布到我的 php 文件?

PS:这是我的控制台错误的全文:

POST http://localhost:3000/together.php 404 (Not  Found)x.ajaxTransport.x.support.cors.e.crossDomain.send @ jquery.min.js:6x.extend.ajax @ jquery.min.js:6x.each.x.(anonymous function) @ jquery.min.js:6addTagAutoTogether @ global.js:190x.event.dispatch @ jquery.min.js:5x.event.add.y.handle @ jquery.min.js:5

点击 link 会创建一个 GET 请求,但您的代码会创建一个 POST 消息。您的服务器 API 显然不知道 url 的任何 POST,所以它 returns 是 404。 我不知道你的 API 的后端,但也许你必须先设置一个 (POST) 路由。