AJAX请求json失败
AJAX request json fail
我的ajax请求有问题,当我用属性dataType:'json'进行请求时,我的响应出现了一个错误,parserror,我在PHP中的函数返回了类似json_encode()的数据,你能帮我吗?当我在没有属性dataType:'json'的情况下执行请求时,我的数据是ALL the DOCUMENT HTML。
我的请求:
var dataAr = {Latitude: Latitude, Longitude: Longitude};/
console.log(dataAr);
$.ajax({
data: dataAr,
dataType: 'json',
type: 'POST',
url: 'http://localhost/GPS/Server.php/GPS/Coords',
success: function (data, response) {
console.log('Data: '+data);
console.log('Response: '+response);
},
error: function (textStatus, errorThrown) {
console.log('Status: '+textStatus);
console.log('Error: '+errorThrown);
}
});
我在PHP中的函数:
class GPS
{
function Coords()
{
$Res=$_POST['data'];
$Latitude=$_POST['Latitude'];
$Longitude=$_POST['Longitude'];
return json_encode($Res);
}
}
尝试使用content-type
:
function Coords()
{
$Res=$_POST['data'];
$Latitude=$_POST['Latitude'];
$Longitude=$_POST['Longitude'];
header('Content-Type: application/json'); // <-- HERE
return json_encode($Res);
}
$_POST变量的名称与您发送的名称相同,而不是"data"。目前还不清楚您想要返回什么,所以出于示例目的,以下只是返回并用输入值数组:
class GPS
{
function Coords()
{
$Latitude=$_POST['Latitude'];
$Longitude=$_POST['Longitude'];
$result = array( $Latitude, $longitude );
header('Content-Type: application/json');
echo json_encode($result);
}
}
相关文章:
- 当json解析空响应时,Whatwg-Fetch失败,我该如何防止它
- 当其他解析器认为意外的令牌有效时,json.parse会失败
- $.ajax调用失败,dataType为json
- 如何避免JSON.stringify在特殊情况下返回undefined,从而为JSON.parse创建字符串失败
- 当字符串包含 HTML 标记时,JSON 解析失败
- 在什么情况下,应该.deep.equal失败,但使用JSON.stringify进行比较工作正常
- 使用 JavaScript 解析 json 失败
- $http-get方法即使使用AngularJS中.Net WebApi的有效json也会失败
- zombie.js-xhr-json总是失败
- AJAX请求json失败
- 从php获取javascript中的json对象失败
- Angular JS,把json放到ng重复出一个事件失败
- Google 脚本:JSON.parse 无缘无故失败
- JSON 失败并继续执行代码
- d3.json有效,但$.getJson失败
- JSON.parse 失败,并显示有效的 JSON
- 解析 JSON 失败,JavaScript 中缺少元素
- 字符串解析、JSON Stringify 和 GSON 日期解析失败
- JSON.parse无缘无故失败
- JSON.stringify 在新创建的对象(node、javascript)上失败