jason
jason

Reputation:

jQuery ignores response

Hi anyone sees why my alert won't work?

<script type="text/javascript">
    function SubmitForm(method) {
        var login = document.form.login.value;
        var password = document.form.password.value;
        $.post("backend.php", { 
            login: login, 
            password: password, 
            method: method},
            function(data){
            alert(data.message); 
            console.log(data.refresh); 
        }, "json");
    }
</script>

Response from backend.php is

backend{"message":"Log in credentials are not correct","refresh":"false"}

Upvotes: 1

Views: 149

Answers (3)

jason
jason

Reputation:

Great_llama was right, for some reason I had a 'backend' echoed further up the script. Removed this and all go.

Upvotes: 0

Cody Caughlan
Cody Caughlan

Reputation: 32748

While I dont think its your problem, the callback function does take a 2nd parameter (the jQuery docs call it "textStatus") which is a textual representation of the HTTP status. Specify a 2nd argument to your callback.

function(data, textStatus) { ...

Upvotes: 0

great_llama
great_llama

Reputation: 11729

Why is 'backend' at the start of your response? I would start by removing that. Everything from { to } looks good.

Upvotes: 1

Related Questions