2013-09-03 02:05
浏览 43


I have a PHP file myFile.php that echoes a random string from an array of strings into the screen. I want to run that php file and have the output pop up in a javascript alert. I took a crack at the syntax with alert($.ajax({ url: 'getRejection.php' }));

But it is not quite right , it alerts [object Object].

How do I do this?

图片转代码服务由CSDN问答提供 功能建议

我有一个PHP文件 myFile.php ,它回显一个数组的随机字符串 字符串进入屏幕。 我想运行该php文件并在javascript警报中弹出输出。 我用 alert($。ajax({url:'getRejection.php'}));

对语法进行了修改但是它不是很正确 ,它会提醒 [object Object]


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongshuql24533 2013-09-03 02:07

    According to the jquery documentation for ajax:

    You want to use the success callback to run when the ajax call succeeds.

        url: 'getRejection.php',
        success: function(data) { alert(data) }

    Also, a note of advice: use full paths for urls, not relative url paths. Like this url: '/getRejection.php' not like this url: 'getRejection.php'

    打赏 评论
  • dsfb20227 2013-09-03 04:13

    You may use JSON by making your php file to echo its output in JSON format as follows:

    echo "{
      echo "\"txt\":";
      echo $TheRandomText;
      echo "
      echo "}";

    The previous step may be easily done as Joe Frambach suggestion like the following:

    echo json_encode(array('txt' => $TheRandomText));

    And then using Jquery do the following:

              format: "json"

    However you have to get assured from the path to getRejection.php. In this case it is supposed to be at the same directory with your file.

    Also you have to be sured that getRejection.php does not echo anything else text in JSON format to void the corruption of JSON query.

    打赏 评论

相关推荐 更多相似问题