donvo24600
donvo24600
2013-05-01 10:59

不能让跨浏览器的AJAX请求工作

  • php
  • ajax
  • jquery

I am doing a simple ajax request to another domain like this:

<script type="text/javascript">
$(function() {
    $('.clik').click(function() {
        $.ajax({
              type: "POST",
              url: "http://sub.mydomain.com/test.php",
              crossDomain: true,
              dataType:"jsonp",
              success: function(data) {

                    $('p.txt').html(data['no']);

              }

        });
    });
});
</script>
</head>

<body>
<p class="clik">Halleluja</p>
<p class="txt"></p>

this is the test.php page on sub.mydomain.com

<?
header('Access-Control-Allow-Origin: http://mydomain.com');
// Begin Session
require_once('cl.session.php');
$session = new Session();
$session->start_session('test', false);

// Access Database
require_once('cl.database.php');
$login_db = new Database('user', 'pass', 'accounts', 'test');
$login_pdo = $login_db->PDO;

include "fn.check_login.php";
if(checkLogin($login_pdo) == true) {

    // We start out by checking if the request has been made using AJAX
    if (is_ajax()) {

        echo "this is working";

    } else {

        echo "this is not working!";

    }

} else {
   echo 'You are not authorized to access this page, please login. <br/>';
}

// Function to check if the request is an AJAX request
function is_ajax() {

    // BOOLEAN return if AJAX
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';

}
?>

It returns a semantic issue.

Also if I simply echo some basic text:

<?
echo "Hello World!";
?>

it still returns a semantic issue.

could somebody tell me what went wrong?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答