donvo24600 2013-05-01 10:59
浏览 60
已采纳

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

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条回答 默认 最新

  • doubianyan9749 2013-05-01 11:05
    关注

    Well, for a start, JSONP requests can't be POST (only GET). But I tend to assume jQuery is ignoring the invalid type. JSONP is intrinsically a GET.

    Your response to it is invalid. You've told jQuery you're expecting the server to provide a JSONP response. but your responses aren't JSONP.

    A JSONP response would look something like this:

    callback({
       "property": "value",
       "anotherProperty": 42
    })
    

    ...where the name of the callback (callback in the above) is taken from the query string of the request. So for instance, if the request were http://sub.mydomain.com/test.php?callback=foo, the response would use foo for the name of the callback:

    foo({
       "property": "value",
       "anotherProperty": 42
    })
    

    jQuery will add the callback= query string parameter to the request for you automatically, and generate the corresponding function for you, which in turn calls the ajax success handler with the data passed into it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python