donglu8334 2015-04-05 02:53
浏览 8
已采纳

Ajax按钮php变量[关闭]

I'm trying to make a ajax button to run a link with php code. In the code below when I click the button it gives a refresh on the page and does not run the line:

$.ajax({url: 'http://192.168.0.106:8080/v1/devices/'<?php $deviceID; ?>'/led/?access_token='<?php $dispositivo ?>'?params=l1,HIGH', success: function(result){

I want it to only run the line, without refreshing the entire page, where is the problem?

<?
echo "<form method='post' action='test.php'>";
?>

<script>
$(document).ready(function(){
    $("button").click(function(){
        $.ajax({url: 'http://192.168.0.106:8080/v1/devices/'<?php  $deviceID; ?>'/led/?access_token='<?php $dispositivo ?>'?params=l1,HIGH', success: function(result){
            $("#div1").html(result);
        }});
    });
});
</script>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

<button>Liga</button>
<?
     echo "</form>";//fim do formulário
?>
  • 写回答

1条回答 默认 最新

  • douze2890241475 2015-04-05 02:56
    关注

    remove apostrophe (')

    $.ajax({url: 'http://192.168.0.106:8080/v1/devices/<?php echo $deviceID; ?>/led/?access_token=<?php $dispositivo ?>?params=l1,HIGH', success: function(result){
    

    or

    $.ajax({url: 'http://192.168.0.106:8080/v1/devices/'+<?php echo $deviceID; ?>+'/led/?access_token=<?php $dispositivo ?>?params=l1,HIGH', success: function(result){
    

    this string is Javascript string

    'http://192.168.0.106:8080/v1/devices/'+<?php echo $deviceID; ?>+'/led/?access_token=<?php $dispositivo ?>?params=l1,HIGH'
    

    and you must use echo to add php string to it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应