2012-07-04 58 views
0

我的代碼如下:如何使用jQuery ajax發送GET值?

jQuery.ajax({ 
    url: '/Control/delete', 
    type: 'GET', 
    contentType: 'application/json', 
    success: function (bool) { 
     if (bool == "deleted") { 
      alert('record deleted'); 
      $(".row" + currentId).hide('slow'); 
     } 
     else { 
      alert('not deleted '); 
     } 
    } 
}); 

AAY比如我需要發送的file_id paramater使用GET,我怎麼能做到這一點(的file_id = 12?)?

回答

3

使用data參數

jQuery.ajax({ 
    url: '/Control/delete', 
    type: 'GET', 
    contentType: 'application/json', 
    data: {file_id: 12} 
    success: function (bool){ 
    if(bool == "deleted"){ 
    alert('record deleted'); 
    $(".row"+currentId).hide('slow'); 
    } 
    else{ 
    alert('not deleted ');     
    } 
} 
}); 

而且不是data可以還查詢字符串,如:

data: "file_id=12&foo=bar" 

在情況下,如果它不是一個查詢字符串,jQuery將自動將其轉換爲查詢字符串。

要發送到服務器的數據。它被轉換成查詢字符串,如果還不是字符串的話。

jQuery.ajax docs

0

只需將它添加到URL:

url: '/Control/delete?file_id=12', 
3

使用data選項:

jQuery.ajax({ 
    type: 'GET', 
    data: {file_id : 12}, 
    ...... 
}); 

http://api.jquery.com/jQuery.ajax/

+0

這實際上是發佈 – coolguy

+0

實際GET方法'數據: 「的file_id = 12&一些其它= othervalue」' – coolguy

+1

@ubercooluk:不,它不是,'類型: 'GET',' – Blaster

1

使用此與/ URL替換URL /刪除?的file_id = 12

jQuery.ajax({ 
     url: '/Control/delete?file_id=12', 
     type: 'GET', 
     contentType: 'application/json', 
     success: function (bool){ 
     if(bool == "deleted"){ 
     alert('record deleted'); 
     $(".row"+currentId).hide('slow'); 
     } 
     else{ 
     alert('not deleted ');     
     } 
    } 
    }); 
0

使用Ajax調用的data選項,並與您的鍵值對傳遞給它的對象。

0
//POST METHOD 

$.ajax({ 
    type: 'POST', 
    data: {file_id : 12}, 
    ...... 
}); 

//GET METHOD 

$.ajax({ 
    type: 'GET', 
    data: "file_id=12&someother=othervalue", 
    ...... 
});