Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX在发送帖子和获取结果方面存在问题_Javascript_Jquery_Json_Ajax_Web - Fatal编程技术网

Javascript AJAX在发送帖子和获取结果方面存在问题

Javascript AJAX在发送帖子和获取结果方面存在问题,javascript,jquery,json,ajax,web,Javascript,Jquery,Json,Ajax,Web,我是JS和JSON的新手。因此,我需要使用POST向Google API发送一个JSON消息,并获取答案并通过警报显示。我写了这个页面,但是这个代码没有结果 <!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=

我是JS和JSON的新手。因此,我需要使用POST向Google API发送一个JSON消息,并获取答案并通过警报显示。我写了这个页面,但是这个代码没有结果

<!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>

重新定价
$.ajax({
类型:“POST”,
网址:'https://www.googleapis.com/geolocation/v1/geolocate?key=KEY',
数据:{WIFI接入点:[“macAddress”,“signalStrength”],手机发射塔:[“cellId”,“locationAreaCode”,“mobileCountryCode”,“mobileNetworkCode”]},
数据类型:“json”,
成功:功能(响应){
警报(响应);
}
});

使用AJAX时,您需要设置所发送信息的内容类型。默认情况下,该值设置为
application/x-www-form-urlencoded;字符集=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);
    }
  });

您是否以JSON格式发送响应?datatype属性是指从服务器返回的类型。如果不匹配,控件将返回错误回调。删除或更新datatype属性以匹配响应类型。进行以下更改并重试

$.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");
        }
    });

似乎您没有传递json数据,但数据属性中的字符串值您的数据对象应该是
数据:{wifiAccessPoints:[“macAddress”,“signalStrength”],cellTowers:[“cellId”,“locationAreaCode”,“mobileCountryCode”,“mobileNetworkCode”]}
,//删除单引号around@serdar.sanri谢谢,这是个错误。但是现在没有响应添加错误处理程序并显示结果,请使用开发人员工具确定服务器对ajax帖子的响应。当我添加我的密钥并测试它时,我得到一个400错误响应。你这边可能会不一样。尝试添加
success:function(){//your code},error:function(details){console.log(details)}
以确定您收到的实际错误。感谢@kevin-b捕获请求正文问题。