2010-03-14 100 views
2

我正在學習jQuery並嘗試以下操作,但參數對我來說很陌生,所有嵌入的引號我都認爲這是我的問題。有人可以解釋的參數和引號去哪裏,並可能重寫我的參數線? (這是一個活網站,查看所需的參數)。找不到jQuery ajax調用參數

function AirportInfo() { 
var divToBeWorkedOn = '#detail'; 
var webMethod = "'http://ws.geonames.org/citiesJSON'"; 
var parameters = "{'north':'44.1','south':'9.9','east':'22.4','west':'55.2','lang':'de'}"; 
$.ajax({ 
    type: "POST", 
    url: webMethod, 
    data: parameters, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
     alert(msg.d); 
     $(divToBeWorkedOn).html(msg.d); 
    }, 
    error: function(xhr) { 
     alert(xhr); 
     alert(xhr.statusText); 
     alert(xhr.responseText); 
     $(divToBeWorkedOn).html("Unavailable"); 
     } 
}); 
} 

回答

0

我總是寫的參數是這樣的:

data: "north=33.4&south=3"..... , 
1

嘗試這種方式

var divToBeWorkedOn = '#detail'; 
    var webMethod = "'http://ws.geonames.org/citiesJSON'"; 
    var parameters = {'north':'44.1','south':'9.9','east':'22.4','west':'55.2','lang':'de'}; 

$.ajax({ 
    'type': "POST", 
    'url': webMethod, 
    'data': parameters, 
    'contentType': "application/json; charset=utf-8", 
    'dataType': "json", 
    'success': function(msg) { 
     alert(msg.d); 
     $(divToBeWorkedOn).html(msg.d); 
    }, 
    'error': function(xhr) { 
     alert(xhr); 
     alert(xhr.statusText); 
     alert(xhr.responseText); 
     $(divToBeWorkedOn).html("Unavailable"); 
    } 
}); 
1

我總是用下面的方式。看看是否適合你。我改變了你的代碼,使其更像我:

var divToBeWorkedOn = '#detail'; 
var webMethod = "http://ws.geonames.org/citiesJSON"; 
var parameters = { north:'44.1',south:'9.9', east:'22.4', west:'55.2',lang:'de' }; 

$.ajax({ 
    type: "POST", 
    url: webMethod, 
    data: parameters, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
     alert(msg.d); 
     $(divToBeWorkedOn).html(msg.d); 
    }, 
    error: function(xhr) { 
     alert(xhr); 
     alert(xhr.statusText); 
     alert(xhr.responseText); 
     $(divToBeWorkedOn).html("Unavailable"); 
    } 
});