简体   繁体   中英

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:

echo 'Hello, world'

And I want to place that text in a 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:

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

This places the HTML output from test.php directly into the #div_1 element.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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