How to appending url path in jquery

时间:2016-10-20 18:54:22

标签: javascript jquery ajax

Using $(this).attr("href"); in jQuery Ajax url field gives the URL path but when i use a prefix in front of it like this:

$.ajax({
    type: 'GET'
    url: 'api/'+ $(this).attr("href");
}) 

it doesn't work.

Whats going wrong? Is this wrong method or is there a better way?

1 个答案:

答案 0 :(得分:3)

$(this) -> will return a wrong result in the object.

Get the value in another place and add the ready result;

var getURL = 'api/'+ $(this).attr("href"); // In the level,where your $(this) is the actual this

$.ajax({
    type: 'GET',
    url: getURL
});