php file send variable to .js external file

2019-09-17 03:54发布

From jquery file to php:

$.post('http://botk-online.com/play2.php', {
 'rate': Pontuacao.misses
});

How to modyficate this code to pass $data variable from php file to js external file and read it value.

2条回答
The star\"
2楼-- · 2019-09-17 04:01

PHP

<?php

  $result = array('data' => 'this is some data');
  echo json_encode($result);

Javascript:

$.ajax({
    url: 'http://botk-online.com/play2.php',
    type: 'post',
    data: {
        rate: Pontuacao.misses
    },
    dataType: 'json',
    success: function(json) {
        alert('Data is: '+json['data']);
        // Alerts: 'Data is: this is some data'
    }
});
查看更多
来,给爷笑一个
3楼-- · 2019-09-17 04:08

Use the success handler, which will run if the code returns successfully:

jQuery.post( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )

Note the , success(data, textStatus, jqXHR) above. For example:

$.post('http://botk-online.com/play2.php', {
        'rate': Pontuacao.misses
    }, function (data) {
        doStuff(data);
    }
});

http://api.jquery.com/jQuery.post/#example-5

查看更多
登录 后发表回答