doushang2571 2016-03-10 16:18
浏览 606

jQuery $ .ajax发布referer url进行错误调试

i want to create a function for sending error messages from $.ajax posts via email to myself, here's my code:

    $.ajax({
        type: "POST",
        url: my_url,
        data: data,
        beforeSend: function(request, settings) {
            request.settings = settings;
        },
        success: function(data) {
        },
        error: function (request, status, error)
        {
            send_error(status, error, request);
        }
    });

i mapped the request.settings into the ajax call in order to receive extra infos for my send_error function. i'm getting infos like url, type, data, responseText (= server's error message). i would like also getting the referrer URL so i can see where it was triggered. seems it's only inside the responseText, a link inside the server's error message. is there a better alternative for getting the referrer URL? thanks

  • 写回答

1条回答 默认 最新

  • doushuichong2589 2016-03-10 16:29
    关注

    You can use PHP to get the referer, however the variable I'm using is unreliable and can be spoofed but then again, everything in Javascript can be spoofed too.

    error: function (request, status, error){
        send_error(status, error, request, "<?php echo $_SERVER['HTTP_REFERER'] ?>");
    }
    

    An alternative to this is to track the user over your website using sessions. Append each $_SERVER['REQUEST_URI'] in a $_SESSION['referer'] array and print the second last index directly to Javascript.

    评论

报告相同问题?