Home  >  Article  >  Backend Development  >  ajax - PHP interface problem

ajax - PHP interface problem

WBOY
WBOYOriginal
2016-10-17 09:30:15980browse

I want to make a simple page display statistics. The rookie has one front end, but not the back end. I wrote a small case and ran it on github, but it was met with disappointment. I’m here to ask the experts from all walks of life. Let’s stop talking nonsense and get down to the code.

ajax:

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

json:

<code>{
    "vp":"0"
}</code>

php:

<code> <?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;
 ?></code>

The idea is that every time you enter a page and make a request, PHP will read and write json records (the database will not). But the ajax request fails every time. But the PHP request status code is 200.

ajax - PHP interface problem

ajax - PHP interface problem

ajax - PHP interface problem

ajax - PHP interface problem
How to export the entire file?

Reply content:

I want to make a simple page display statistics. The rookie has one front end, but not the back end. I wrote a small case myself and ran it on github, but it was met with disappointment. I’m here to ask the experts from all walks of life. Let’s stop talking nonsense and get down to the code.

ajax:

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

json:

<code>{
    "vp":"0"
}</code>

php:

<code> <?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;
 ?></code>

The idea is that every time you enter a page and make a request, PHP will read and write json records (the database will not). But the ajax request fails every time. But the PHP request status code is 200.

ajax - PHP interface problem

ajax - PHP interface problem

ajax - PHP interface problem

ajax - PHP interface problem
How to export the entire file?

Click on the index.php request to see what the response is and whether it meets the requirements

php language interpreter execution.
Install a wamp and test again.

Haha, the json data returned is obviously an object, but it has to be accessed using an array
$data['vp'] = intval($data['vp']) + 1; change to $data->vp = $ data->vp + 1;
My answer is absolutely correct, if you don’t believe me, give it a try

This should be a problem with the configuration of the web server, which does not parse the php file.

github does not have any server environment

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn