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 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的