使用jQuery将数据发送到绝对URL

Cri*_*jon 3 ajax url jquery post

我有一个带有URL的HTML文件localhost/dir1/dir2/file.html.在这个文件中,我通过jquery将数据发送到服务器.

$('#element').live("change", function(){
    $.ajax({
        url:  "localhost/dir1/dir3/file.php",
        type: "POST",
        data: { esp: $(this).val() },
        success: function(result){
            $("#element2").html(result);
        }
    })
Run Code Online (Sandbox Code Playgroud)

我得到一个错误,因为jquery发送数据localhost/dir1/dir2/localhost/dir1/dir3/file.php但我需要发送数据localhost/dir1/dir3/file.php.我怎样才能做到这一点?

mcc*_*nnf 5

将您的网址更改为:

   /dir1/dir3/file.php
Run Code Online (Sandbox Code Playgroud)

这样可以避免将协议和服务器硬编码到代码中.