404将JS变量推送到PHP,调用PHP函数,将结果作为JSON字符串传递给JS,并在使用它时可能失败
So I've found like gazillion of StackOverflow questions and answers to all the subtopics of my title. Linked them together and got 404. I am using MEAN stack to build simple API searching app.
This is the echo of 'together.php'(function is working just fine):
$variable1 = $_POST['JavaScriptButtonVariable1'];
$variable2 = $_POST['JavaScriptButtonVariable2'];
echo json_encode(inst_search(myfunction($variable1, $variable2)));
Here is the give part of my 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
Keys interesting for me are: 'Number of instagram submitters this session', 'Total number of tags submitted', 'Score of this hashtag/keyword pair this session' and 'Date and time (YYYY/MM/DD HH:MM:SS)', thus I am using [2],[3],[4] and [5] (visible in the code below). There is only one value assigned to those keys every time.
This is the part of the function in my 'global.js' file which calls the php file:
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()
}
Now, I simplified the code to the parts which may cause the problem, including the 404 error while calling and possibly my extreme inability to correctly construct JS objects from arrays extracted from JSON.
When clicking on the link http://localhost... my together.php gets downloaded, so it is definitely in the right directory.
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3000/together.php
Questions: Why is my console calling 404? Is the code for the array and the object written correctly?EDIT : How to allow POSTing to my php file by routing?
PS: Here's the full text of my console error:
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
Clicking the link creates a GET
request, but your code creates a POST
message. Your servers API apparently doesn't know about any POST for that url, so it returns a 404.
I don't know about the backend of your API, but perhaps you have to set a (POST
)route first.