weixin_33721427 2015-07-05 16:21 采纳率: 0%
浏览 24

Ajax请求始终失败

I'm trying to send variables of username and password to a PHP file from a JS file, but the ajax request always fails (prints fail), no idea why.

JS file:

<script>
    var imported = document.createElement('script');
    imported.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js';
    document.querySelector('head').appendChild(imported);

    function collect() {
        var user = "joe";
        var pass = "123";
        $.ajax({
            type:'POST',
            url: 'myphp.php',
            data: {'username': user, 'password': pass},
            success: function(){
                alert("success!");
            },
            error: function(){
                alert("fail");
            }
        });
    }
</script>

PHP file:

<?php
$myval = $_POST['username'];
echo($myval);
?>
  • 写回答

2条回答 默认 最新

  • hurriedly% 2015-07-05 16:25
    关注
    <script>
        var imported = document.createElement('script');
        imported.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js';
        document.querySelector('head').appendChild(imported);
    
        function collect() {
            var user = "joe";
            var pass = "123";
            $.ajax({
                type:'POST',
                url: 'myphp.php',
                data: {'username': user, 'password': pass},
                success: function(){
                    alert("success!");
                },
                error: function(){
                    alert("fail");
                }
            });
        }
    </script>
    
    
    PHP file:
    
    <?php
    $myval = $_POST['username'];
    echo($myval);
    ?>
    

    When posting with ajax you don't need EQUAL (=), that's the problem you're having.

    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)