php file send variable to .js external file

こ雲淡風輕ζ 提交于 2019-12-12 01:13:38

问题


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.


回答1:


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'
    }
});



回答2:


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



来源:https://stackoverflow.com/questions/8546590/php-file-send-variable-to-js-external-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!