ajax - PHP接口问题

查看:91
本文介绍了ajax - PHP接口问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我想做一个简单的页面展示统计。菜鸟前端一枚,后端不会。自己写了个小case在github跑,碰了一鼻子灰。特来请教各路大神,废话不说,上代码。

ajax:

$.ajax({
    url: 'php/index.php',
    type: 'get',
    dataType: 'json',
    data: {},
})
.done(function(dat) {
    console.log("2");
    $(".header").html(dat.vp);
})   
.fail(function() {
    console.log("error");
})
.always(function() {
    console.log("complete");
});

json:

{
    "vp":"0"
}

php:

 <?php 
    $json_string1 = file_get_contents('../json/index.json');
    $data = array();
    $data = json_decode($json_string1, true);
    $data['vp'] = intval($data['vp']) + 1;
    $json_string2 = json_encode($data);
    file_put_contents('../json/index.json', $json_string2);
    echo $json_string2;
 ?>

想法是每进入一次页面,进行一次请求,PHP读写json记录次数(数据库不会)。可是ajax请求每次都进入fail了。但是PHP请求状态码是200.


怎么把整个文件输出了?

解决方案

点开index.php请求看看response了什么 是不是符合要求

这篇关于ajax - PHP接口问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆