没有在使用在线网址的ajax呼叫中获得响应

时间:2015-01-10 10:07:15

标签: javascript php jquery ajax json

以下是我正在使用的ajax方法。在这个当我使用在线网址时,因为我没有得到任何回复,但是如果我在同一文件夹中使用该文件,则响应。它正在给出响应。从在线访问同一文件没有回复的地方。

<script type="text/javascript">
$("document").ready(function(){
    $(".js-ajax-php-json").submit(function(){
        var data = {
            "action": "test"
        };
        data = $(this).serialize() + "&" + $.param(data);
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "http://www.---.com/projects/app/response.php",
            data: data,
            success: function(data) {
                $(".the-return").html(
                    "Favorite beverage: " + data["favorite_beverage"] + "<br />Favorite restaurant: " + data["favorite_restaurant"] + "<br />Gender: " + data["gender"] + "<br />JSON: " + data["json"]
                );

                alert("Form submitted successfully.\nReturned json: " + data["json"]);
            }
        });
        return false;
    });
});
</script>

以下是response.php文件

<?php
if (is_ajax()) {
    if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
        $action = $_POST["action"];
        switch($action) { //Switch case for value of action
            case "test": test(); break;
        }
    }
}

//Function to check if the request is an AJAX request
function is_ajax() {
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}

function test(){
    $return = $_POST;

    //Do what you need to do with the info. The following are some examples.
    //if ($return["favorite_beverage"] == ""){
    //  $return["favorite_beverage"] = "Coke";
    //}
    //$return["favorite_restaurant"] = "McDonald's";

    $return["json"] = json_encode($return);
    echo json_encode($return);
}
?>

0 个答案:

没有答案
相关问题