0

I am using an MVC approach here. In my javascript code, under the success function.. when I use the console.log(data.msg) code, my expected result comes up.

But when I use the $('#res').text("".data.msg) .. the output is TypeError: "".data is undefined from the console..

And if I use $('#res').text(data.msg)... There is no output in the console..

I expect the result to show in the text field having the id of res.

Here is the html code

<form action="" method="post">
    <input type="text" id="uname">
    <input type="submit" id="submit">

    <input type="text" id="res">
</form>

Here is the javascript code

$(document).ready(function(){

$('#submit').click(function() {

    $.ajax({
        type : 'POST',
        url : 'ajax/checkLogin',
        dataType : 'json',
        data: {
            uname : $('#uname').val(),
        },
        success : function(data){
            $('#res').text("".data.msg);
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert("There was an error");
        }
    });

    return false;
});
});

And here is the method code of the controller named Ajax

public function checkLogin()
{
    $return['error'] = false;

    while (true) {
        if (empty($_POST['uname'])) {
            $return['error'] = true;
            $return['msg'] = 'You did not enter you username.';
            break;
        }

        if ($_POST['uname'] == "tin") {
            $return['msg'] = 'successful';
            break;
        }else{
            $return['error'] = true;
            $return['msg'] = 'Wrong username:'.$_POST['uname'];
            break;
        }
        break;
    }

    echo json_encode($return);
}
6
  • so what give you console.log(data); ??? Commented Mar 12, 2014 at 12:27
  • And if I use $('#res').text(data.msg)... There is no output in the console.. How this code will output the data in the console..? Commented Mar 12, 2014 at 12:28
  • @A.Wolff [object Object] Commented Mar 12, 2014 at 12:28
  • Don't tell me you are debugging it using IE?! Commented Mar 12, 2014 at 12:28
  • 1
    @Katherine For wolf: console.log(JSON.stringify(data)); Commented Mar 12, 2014 at 12:28

4 Answers 4

2

You do not need dot before data, you probably do not need empty string concatenation with data.

$('#res').text(data.msg);
Sign up to request clarification or add additional context in comments.

Comments

2
$('#res').text("".data.msg);

change this to

$('#res').text(data.msg);

It is quite clear from the error. TypeError: "".data is undefined. Since string doesn't have a property data

4 Comments

thanks.. but there is no output when i tried the $('#res').text(data.msg);
Try to log data and make sure the ajax call is success full. Or show us the expected JSON response
yes i tried to log it.. the ajax call was successful. But the result message doesn't show in the textfield
@Katherine ok #res is an input, not a DIV, so use .val() not .text(). I'm quite sure i saw it as DIV but cannot see any edit for question, strange...
1

You are trying to concatenate string in javascript with the PHP operator. To concatenate two strings, you have to use the + operator not the . operator

Comments

1

If you wanted empty string then concate it by + not by .

$('#res').text(""+data.msg);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.