dongxian8858 2015-07-23 07:41
浏览 92
已采纳

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
  • 写回答

1条回答 默认 最新

  • dousi2251 2015-07-23 08:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器