ajax - PHP接口问题
高洛峰
高洛峰 2017-04-10 17:38:04
0
6
260

我想做一个简单的页面展示统计。菜鸟前端一枚,后端不会。自己写了个小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.


怎么把整个文件输出了?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!