douliao8402 2015-12-02 11:03
浏览 51
已采纳

jQuery $ .post无法处理特定条件

I'm trying to insert some data into mySQL database using AJAX and jQuery $.post. My attempt to test the functionality is as follows

index.html: code fragment (already using jQuery for many other functions)

$.post( 
    "updateDatabase.php",
    { name: "Zara" },
    function(data) {
         //$('#stage').html(data);
         alert('Success');
    }
);

updateDatabase.php

<?php
if( $_REQUEST["name"] ) {

   $name = $_REQUEST['name'];
   echo "Welcome ". $name;
   echo "<script>alert('Im here')</script>";
}
?>

What I'm getting is an alert with "Success" and nothing else(It is the callback function of $.post). I already researched a lot and found some similar but different questions. However my problem left unsolved because there is no satisfactory solution for this particular issue.

  • 写回答

3条回答 默认 最新

  • dougong7850 2015-12-02 11:07
    关注

    You are echoing a string echo "alert('Im here')";. so,

    1. It can't be treated as a javascript function.
    2. You are not using the response in the success callback.

    What you can do is:

    function(data) {
         alert('Success: '+data);
         // outputs: Success: Welcome Zara I'm here
    }
    

    and at php:

    echo "Welcome ". $name . " I'm here";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵