简体   繁体   中英

jQuery-Ajax: Append data in URL

I'm working with Ajax using jQuery and I want to append data on the URL. Here's the sample code:

var my_site= mysamplesite.com/viewData/";
function showData(myData)
{
    $.ajax({
        type: "Get",
        url: my_site+"getData"
        data:{
            myData: myData
        },
        dataType: "json",

        success: function(data_details){
            $('#data-target').html(data_details.data1+ "\n" +data_details.data2+ "\n" +data_details.data3);
        }
    })
}

These function works smoothly but, as I've observed the URL doesn't change (the data doesn't append to the URL) whenever these function is called. So, I want these data to be appended to the URL whenever this function is called. For example, if the value of data is 1222345 , then I want my URL to be like this:

mySampleSite.com/viewData/getData?myData=1222345

in which the data is appended to the URL. Any help is appreciated. Thanks

The easiest solution is probably just to append the URL parameter manually instead of using the data parameter of the ajax call.

var my_site= "mysamplesite.com/viewData/";
function showData(myData)
{
    $.ajax({
        type: "GET",
        url: my_site + "getData?myData=" + myData;
        success: function(data_details){
            $('#data-target').html(data_details.data1+ "\n" +data_details.data2+ "\n" +data_details.data3);
        }
    })
}

If you need to do it through the data parameter I think your JSON object must be formatted like this: data:{ "myData": myData } (note the added quotes).

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