drtoaamk20278 2016-07-07 23:16
浏览 9
已采纳

使用ajax访问另一个文件

4 weeks ago I wrote a php script which adds products to a cart. As I am new to javascript, I decided to make it better by page loading using ajax.

My work looks like this:

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>

<a href="#" class="cart-box" id="cart-info" title="View Cart">
    <?php
    if(isset($_SESSION["products"])){
        echo count($_SESSION["products"]);
    }else{
        echo 0;
    }
    ?>
</a>

<form class="form-item">
    <div class="cart">
        <input type="submit" value="Add to Cart" class="button" />
    </div>
</form>


<script>
    $(document).ready(function(){

        $(".form-item").submit(function(e){
            var form_data = $(this).serialize();
            $("input[type=submit]").val('Adding...'); //Loading button text

            $.ajax({ //make ajax request to cart_process.php
                url: "test2.php",
                type: "POST",
                dataType:"json", //expect json value from server
                data: form_data
            }).done(function(data){ //on Ajax success
                $("#cart-info").html(data.items); //total items in cart-info element
                $("input[type=submit]").val('Add to Cart'); //reset button text to original text
                alert("Item added to Cart!"); //alert user
            })
            e.preventDefault();
        });
    });
</script>

It seems like the code stops working when I start making the ajax request to test2.php because I can't access the file test2.php and I do not really know where the error is coming from.

Thanks for helping

</div>
  • 写回答

2条回答 默认 最新

  • doubi1910 2016-07-07 23:29
    关注

    Yeah, debugging AJAX can be a bother . . . unless you find a way to "see" what's going on over there.

    Two ideas:

    (1) In PHP file, create a new file, write log entries to the file, close file when done. You can run the routine and check the file you created.

    $fq=fopen('_myeyes.log','a');
    fwrite($fq, '***** Created by uploader.php *****' . "
    " ); 
    fwrite($fq, '$fname: ' .$fname . "
    " ); 
    fwrite($fq, '$pathToImages: ' .$pathToImages. "
    " ); 
    fwrite($fq, '$pathToThumbs: ' .$pathToThumbs. "
    " ); 
    fwrite($fq, '$thumbWidth: ' .$thumbWidth. "
    " ); 
    fclose($fq);
    

    It works, but there's a better way.

    (2) Install the Firefox extension (there's also one for Chrome, but the ff one is more reliable -- so if you're a Chrome addict like I am then use both:

    firePHP

    FirePHP for Chrome

    Get the FFox one working first, because sometimes the Chrome one is glitchy - which is fine when you know it works, but is intimidating when you're just trying it out. You think your code is problematic, but it's the extension...


    A Guide To Using FirePHP

    Debugging PHP Code With FirePHP


    Basically, after downloading FirePHP:

    (1) Upload these files into a folder called public_html\FirePHPCore (where public_html is your webroot, as is common on most web hosting)

    fb.php
    fb.php4
    FirePHP.class.php
    FirePHP.class.php4
    

    (2) At top of PHP file, these lines:

    <?php
    if (file_exists('../FirePHPCore/fb.php')) {
        require_once('../FirePHPCore/fb.php');
    }else{
        if (file_exists('FirePHPCore/fb.php')) {
            require_once('FirePHPCore/fb.php');
        }else{
            $fpLog = fopen('__fph_log.log', 'w');
            fwrite($fpLog, '***** FirePHP did not load - FirePHPCore/fb.php NOT FOUND *****' . "
    "); 
        }
    }
    ob_start();
    $console = FirePHP::getInstance(true);
    $console->registerErrorHandler();
    $console->registerExceptionHandler();
    

    Then, inside any function where you wish to use FirePHP:

    function somefunction(){
        global $console;
        //a bunch of PHP goes here
        $console->log('role: '.$fr);
        //a bunch of PHP goes here
    }
    

    In Chrome/Firefox DevTools (F12), you will see these messages appear in the Console tab, same as if you issued a console.log("Hello there") in javascript.

    You have eyes!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line