2017-08-11 46 views
-2

即時通訊和JSON新的。所以我需要使用POST向Google API發送大量JSON,並通過警報獲取答案並顯示它。我寫了這個頁面,但是這段代碼沒有結果。發送POST的AJAX問題,並獲得結果

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Redericting</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
</head> 
<body> 
    <script type="text/javascript"> 
    $.ajax({ 
     type: "POST", 
     url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=KEY', 
     data: '{wifiAccessPoints:["macAddress", "signalStrength"], cellTowers:["cellId", "locationAreaCode", "mobileCountryCode", "mobileNetworkCode"]}', 
     dataType: "json", 
     success: function(response) { 
      alert(response); 

     } 
    }); 

    </script> 
</body> 
</html> 
+0

看來你是不是傳遞JSON數據,但字符串值數據屬性 –

+0

你的數據對象應該是'數據:{wifiAccessPoints:「MACADDRESS」,「signalStrength」],發射塔: [「cellId」,「locationAreaCode」,「mobileCountryCode」,「mobileNetworkCode」]},//刪除單引號 –

+0

@serdar.sanri謝謝你,這是錯誤的。但現在也沒有迴應 – Aleksandr

回答

0

使用AJAX時,您需要設置您發送的信息的內容類型。默認情況下,該值設置爲application/x-www-form-urlencoded; charset=UTF-8

$.ajax({ 
    type: "POST", 
    url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=KEY', 
    data: JSON.stringify({wifiAccessPoints:["macAddress", "signalStrength"], cellTowers:["cellId", "locationAreaCode", "mobileCountryCode", "mobileNetworkCode"]}), 
    contentType: "json", 
    success: function(response) { 
     alert(response); 
    } 
    }); 
+0

感謝@ kevin-b捕獲請求正文問題。 –

-1

您是否以JSON格式發送回覆?數據類型屬性是指您從服務器返回的類型。如果這不匹配,控制將進行錯誤回調。刪除或更新數據類型屬性以匹配響應類型。做下面的變化和嘗試

$.ajax({ 
     type: "POST", 
     url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=KEY', 
     data: '{wifiAccessPoints:["macAddress", "signalStrength"], cellTowers:["cellId", "locationAreaCode", "mobileCountryCode", "mobileNetworkCode"]}',    
     success: function(response) { 
      alert(response); 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
      alert("error"); 
     } 
    });