doumao1047 2014-05-05 14:53
浏览 86

我的AJAX调用不起作用

I have a simple JavaScript code like this:

var xhr = new XMLHttpRequest, action = "action=latest", page = "http://192.168.1.115/wp-content/themes/HSV%20Saints/setphoto.php";
    xhr.open("POST", page, true);
    xhr.setRequestHeader("Content-type", "application-x-www-formurlencoded");
    xhr.send(action);

    xhr.onload = function(){
            console.log("XHR Onload");
            console.log(xhr.responseText);
            console.log(action);
    }

I have a PHP script like this:

<?php

$action = $_POST['action'];
echo $action;
echo 'Test message';
?>

The $action variable is not showing up, but the 'Test message' is (in the console). I don't understand why it can't send var action

  • 写回答

2条回答 默认 最新

  • dongyunque2511 2014-05-05 14:59
    关注

    Send method of XMLHttpRequest takes data as argument only in case of POST request. Look here https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest For GET, your action should be part of URL.

    评论

报告相同问题?

悬赏问题

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