简体   繁体   中英

How can I send AJAX request on URL

Hi I am not sure if you guys will undestand me because I am spanish and I speak little english but I'll try it: I would like when user sends in URL browser like http://www.facebook.com/profile.php?id=000000001 the browser don't send a request to the server,and AJAX loads the content inside of a DIV. How can I make it work? I hope you can help me and undertand me. Thanks

The browser will always reload the page if you change the address. The Ajax way of handling page changes is to work with the hash tag, which is everything after the pound sign ( # )

www.example.com#page=one www.example.com#page=two www.example.com#page=three&more=params

Thats a complex issue, you can ether use hashbangs ( http://www.facebook.com/profile.php#!000000001 ) because they don't force the browser to (re)load a page, but you can catch them via AJAX if the page is loaded. If you don't want to use hashbangs the HTML5 History Interface http://www.w3.org/TR/html5/history.html would be a Way to go, but its not supported by many browsers, but i made some working stuff under chrome.

您可以使用Fiddler嗅探通过AJAX发送到服务器的数据,然后可以编写HTTPWebRequest / Response对其进行仿真。

You can use the GET on url with this function to make an Ajax call :

$.extend({
    getUrlVars: function(){
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++){
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function(name){
        return $.getUrlVars()[name];
    }
});

And parse arguments :

$(document).ready(function(){
    // load ajax call
    if( $.getUrlVar('param') != null ){
        $.ajax({
            data: {
                something_var: $.getUrlVar('param')
            },
            success: function(data){
              alert(data);
            }
        });
    }
});

Example url : http://www.website.com/?param=ok

Hope it's what you want :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM