简体   繁体   English

从 php 到 jquery/ajax 中获取回显或返回字符串的最简单方法是什么

[英]What is the easiest way to get an echoed or returned string from php into jquery/ajax

Let's say that I have in my test.php:假设我在 test.php 中有:

echo 'Hello, world'

And I want to place that text in a div.我想将该文本放在一个 div 中。

This doesn't work for me and the alert also does not pop out.这对我不起作用,警报也不会弹出。

$.ajax({
   type: "GET",
   url: "test.php",
   data : data,
   success: function(msg){
     alert('Does it Work?');
     ("#div_1").html(message);
 });

You have a syntax error, that may be the issue (you forgot to close a curly bracket and a parenthesis).您有语法错误,这可能是问题所在(您忘记关闭大括号和括号)。 Try尝试

$.ajax({
    type: "GET",
    url: "test.php",
    data : data,
    success: function(msg){
      alert('Does it Work?');
      $("#div_1").html(message.txt);
    }
});
$.get('test.php',function(data) { alert('data:'+data); });

The easiest way is to use the load function:最简单的方法是使用load function:

$("#div_1").load("test.php", data);

This places the HTML output from test.php directly into the #div_1 element.这会将 test.php 中的 HTML test.php直接放入#div_1元素中。

$.ajax({
   type: "GET",
   url: "test.php",
   data : data,
   success: function(msg){
     alert(msg);
     $("#div_1").html(msg);
   }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM