0

I spend already some hours on this problem and cannot find solution. I have the following service on the webpage (kohana 3.2):

public function action_test() {
    $data = file_get_contents('php://input');
    $json = json_decode($data);

    $t = array();
    $t[Utils::$KEY_LOGIN]       = isset($json->login) ? $json->login : arr::get($_POST, Utils::$KEY_LOGIN, null);
    $t[Utils::$KEY_PASSWORD]    = isset($json->password) ? $json->password : arr::get($_POST, Utils::$KEY_PASSWORD, null);

    $returned = array("success" => true);
    $returned = array_merge($returned, array("login" => $t[Utils::$KEY_LOGIN]));

    $this->auto_render = FALSE;
    ob_clean();
    $this->request->headers['Content-Type'] = "application/json";
    $this->response->body(json_encode($returned));
}

And the form to ask the service:

<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="//media/js/jquery-1.6.2.min.js">    </script>
    <script type="text/javascript" src="//media/js/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#login_sb").click(function() {
                //var action = 'http://www.aaa.pl/clientservices/test';
                var action = "http://localhost/aaa/clientservices/test";
                $.ajax({
                    url : action,
                    type : "post",
                    dataType : "json",
                    cache : "false",
                    timeout: 8000,
                    data : {
                        'login': 'login@wp.pl'
                    },
                    success : function(data) {
                        alert(data);
                        if(data.success) {
                            alert(data.login);
                        }
                    } 
                });
// 
                return false;
            });
        });

    </script>
</head>
<body>
    <form id="login_form" method="post">
        <input type="submit" value="login" id="login_sb"/>
    </form>

</body>

When I call the local service everything works ok. But when I am trying to call it by Ajax remotely - it doesn't work. It also works when I manually send the form. Please help, cause I don't have any new ideas what is going wrong.

Konrad
  • 5
  • 2
  • Are you on the exact same domain and URL mentioned in `var action`? – random Aug 12 '12 at 23:49
  • This could help you http://stackoverflow.com/questions/3897641/can-ajax-request-data-from-a-remote-server – svakak Aug 13 '12 at 15:58
  • 2
    What do you mean - "doesnt work"? Debug it with special extensions like FireBug. You need a server response (body, HTTP status etc). – biakaveron Aug 14 '12 at 05:11

1 Answers1

0

Are you possibly trying to call the AJAX request from a different domain?

If you are trying to call the ajax request at this url: 'http://www.aaa.pl/clientservices/test'

from your local setup you will get errors, the ajax request must be made from the same domain.

a file one localhost can't make an ajax request to www.aaa.pl as it's a different domain and a security issue in javascript.

Andrew
  • 12,617
  • 1
  • 34
  • 48
  • It was exactly as you wrote. We are writing our application in Phonegap. We didn't know that the domain must be the same. No everything works fine - big thanks for help! – Konrad Nov 04 '12 at 12:25