我的ip-address功能不起作用[关闭]

My script:

$(function () {
    $("h2").text($.get("ajax.getIp.php",function(data){ return data; }));
});

and my ajax.getIp.php:

<?php$_SERVER['REMOTE_ADDR']?>

It will say [object Object] in the h2 how can I solve this?

Use the success handler, not the return value

Functions like $.get work with callbacks; the return value is not the response from the server - it's the xhr object.

> $.get()

Object {readyState: 1, setRequestHeader: function, getAllResponseHeaders: function, getResponseHeader: function, overrideMimeType: function…}

See the docs for examples of how to use $.get:

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data ); # <-
  alert( "Load was performed." );
});

Note that in the success handler is where the contents of .result are updated. Applied to the code in the question that would be:

$.get( "ajax.getIp.php", function( data ) {
  $("h2").text( data );
});

The php code isn't echoing an ip address

The output of a php file with these contents:

<?php$_SERVER['REMOTE_ADDR']?>

Is the string:

<?php$_SERVER['REMOTE_ADDR']?>

Because, there isn't any php code in it - <?phpnospace does not start a php block. To output the client's IP use the code:

<?php echo $_SERVER['REMOTE_ADDR']?>

Note the whitespace and echo statement, or

<?= $_SERVER['REMOTE_ADDR']?>

(If using php 5.4 OR earlier versions with php short tags enabled) - in this case the whitespace is optional.

try this

<?php echo $_SERVER['REMOTE_ADDR']; ?>

Your PHP code currently generates no output, you're simply referencing a variable.

You need to output the value on the server using either:

<?= $_SERVER['REMOTE_ADDR']; ?>

or:

<?php echo $_SERVER['REMOTE_ADDR']; ?>