2017-08-11 59 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"); 
     } 
    });