weixin_33716154 2017-04-02 23:06 采纳率: 0%
浏览 17

PHP处理Jquery $ .post()

I am trying to make a chat program, but am stuck on probably the most simple aspect of php. I have the following code in app.js

var msg = "123";
$.post('index.php', {send: msg});

and the following in index.php

<?php
switch($_SERVER['REQUEST_METHOD'])
{
  case 'GET':
      echo "<script type='text/javascript'>alert('GET');</script>";
      break;

  case 'POST':
    $msg = $_POST['send'];
    echo "<script type='text/javascript'>alert('POST');</script>";

}
?>

The code only alerts me about the 'GET' request I receive when loading the page. Whenever I call the $.post function in app.js nothing happens. I don't know if there's an issue with the server or if I linked to my index.php file wrong in the request?

  • 写回答

1条回答 默认 最新

  • weixin_33734785 2017-04-02 23:14
    关注

    The issue is that the jquery $.post function will send the request to the website, but then won't magically display the website output - if you don't tell it to do something with the output from the call it won't do anything.

    To get it to output the response from the site, try doing something like this:

    var msg = "123";
    
    $.post('index.php', {send: msg}, function(response) {
        alert(response);
    });
    

    This provides a callback function to the jQuery POST request to instruct it on how to handle the response from the website.

    jQuery post documentation

    Example

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭