Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ajax调用看不出有什么问题:s_Php_Jquery_Ajax - Fatal编程技术网

Php ajax调用看不出有什么问题:s

Php ajax调用看不出有什么问题:s,php,jquery,ajax,Php,Jquery,Ajax,当我点击按钮加载js文件时,什么也没有发生,我已经单独测试了php文件,没有ajax,只是纯php,这是可行的!所以我认为这里的代码有问题: $(document).ready(function(){ $('#changePasswordNeededButton').click(function(){ $.post("http://example.com/change_password.php", { currentPassword

当我点击按钮加载js文件时,什么也没有发生,我已经单独测试了php文件,没有ajax,只是纯php,这是可行的!所以我认为这里的代码有问题:

$(document).ready(function(){
    $('#changePasswordNeededButton').click(function(){
        $.post("http://example.com/change_password.php",
        {
            currentPassword : $('#currentPassword').val(),
            newPassword : $('#newPassword').val(),
            repeatNewPassword : $('#repeatNewPassword').val()
        },
            function(data){
                if (data.result == 1) {
                    location.href = data.location;
                }
            }, "json" // get content which has been returned in json format
        );
    });
});
此文件的HTML:

<div class='changePassword'>
<div id='changePasswordInner'>
    <form>
        <input id='currentPassword' type='password' name='currentPassword' placeholder='Nuværende adgangskode'>
        <input id='newPassword' type='password' name='newPassword' placeholder='Ny adgangskode'>
        <input id='repeatNewPassword' type='password' name='repeatNewPassword' placeholder='Gentag ny adgangskode'>
        <input id='changePasswordNeededButton' type='button' name='changePasswordNeededButton' value='Skift adgangskode'>
    </form>
    <div id'errorChangePassword'></div>
</div>
</div>

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="../script/change_password.js"></script>
我希望有人能看到这里的错误


谢谢..

先看一下jQuery的$.post文档。实际上,我认为应该使用$.ajax,因为它提供了额外的功能;主要是为成功和失败添加回调的能力

来自jQuery在AJAX上的文档:

   var jqxhr = $.ajax( "example.php" )
  .done(function() {
    alert( "success" );
  })
  .fail(function(jqXHR, textStatus, errorThrown) {
// Add error thrown console.logs here like console.log(textStatus, errorThrown)

    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });

// Perform other work here ...

// Set another completion function for the request above
jqxhr.always(function() {
  alert( "second complete" );
});
检查这条线

'location' => 'index.php',
在那一行的末尾,你的代码没有额外的作用。因为您的脚本需要json并接收格式错误的json字符串


嗯,我只是看不出错误是什么,这在我正在做的另一个网站上运行良好。所以我就不这么认为了如果其中一个if条件失败,那么在发生PHP错误时,您不会添加任何专门针对SQL的错误处理。您是否在Sequel Pro或类似的查询应用程序中检查过SQL查询?
'location' => 'index.php',
        <?php

    if (isset($_POST['currentPassword']) && isset($_POST['newPassword']) && isset($_POST['repeatNewPassword'])) {



     $result = array(
                'result' => 1,
                'location' => 'index.php'
            );

        echo json_encode($result);

    exit(); 
    };

    ?>

     <form>
            <input id='currentPassword' type='password' name='currentPassword' placeholder='Nuværende adgangskode'>
            <input id='newPassword' type='password' name='newPassword' placeholder='Ny adgangskode'>
            <input id='repeatNewPassword' type='password' name='repeatNewPassword' placeholder='Gentag ny adgangskode'>
            <input id='changePasswordNeededButton' type='submit' name='changePasswordNeededButton' value='Skift adgangskode'>
    </form>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){
        $('#changePasswordNeededButton').click(function(){
            $.post("",
            {
                currentPassword : $('#currentPassword').val(),
                newPassword : $('#newPassword').val(),
                repeatNewPassword : $('#repeatNewPassword').val()
            },
                function(data){


                    if (data.result == 1) {
                    location.href=data.location;
                    }


                },"json"
            );
            return false;
        });
    });
    </script>