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 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?