4

I have the following span:

<span class='username'> </span>

to populate this i have to get a value from PHP therefor i use Ajax:

    $('.username').html(getUsername()); 
    function getUsername(){
    $.ajax({
        type: 'POST',
        url: myBaseUrl + 'Profiles/ajax_getUsername',
        dataType: 'json',
        data: {

        },
        success: function(data){
            document.write(data);
        }
    })
}

Now when i debug i see that the returned data (data) is the correct value but the html between the span tags stay the same.

What am i doing wrong?

Little update

I have tried the following:

    function getUsername(){
    $.ajax({
        type: 'POST',
        url: myBaseUrl + 'Profiles/ajax_getUsername',
        dataType: 'json',
        data: {

        },
        success: function(data){
            $('.username').html('RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRr');
        }
    })
}

getUsername();

Still there is no html between the tags (no text) but when i look at the console the method is completed and has been executed.

Answer to the little update

The error was in my Ajax function i forgot to print the actual response! Thank you for all of your answers, for those of you who are searching for this question here is my Ajax function:

    public function ajax_getUsername(){
    if ($this->RequestHandler->isAjax())
    {
        $this->autoLayout = false;
        $this->autoRender = false;
        $this->layout = 'ajax';
    }
    print json_encode($this->currentClient['username']);

}

Do note that i am using CakePHP which is why there are some buildin methods. All in all just remember print json_encode($this->currentClient['username']);

3
  • do you want to get data inside span ? Commented Aug 28, 2013 at 12:23
  • You should never use document.write after the initial page load. Commented Aug 28, 2013 at 12:23
  • did you try directly write to span ? Commented Aug 28, 2013 at 12:24

6 Answers 6

10

The logic flow of your code is not quite correct. An asynchronous function cannot return anything as execution will have moved to the next statement by the time the response is received. Instead, all processing required on the response must be done in the success handler. Try this:

function getUsername() {
    $.ajax({
        type: 'POST',
        url: myBaseUrl + 'Profiles/ajax_getUsername',
        dataType: 'json',
        data: { },
        success: function(data){
            $('.username').html(data); // update the HTML here
        }
    })
}

getUsername();
Sign up to request clarification or add additional context in comments.

3 Comments

The data is "JSON" so probably would have to do something a little different in the html() part.
@epascarello also true, but I'll leave that until the OP specifies it as a requirement.
@MarcRasmussen if the code in the success handler is not being executed it means there is an error somewhere, either client or server side. Check the console, and make sure the response code is 200. If not, try creating an error handler and interrogate the passed parameters to discover the issue.
1

Replace with this

success: function(data){
    $('.username').text(data);
}

Comments

1

In success method you should use something like this:

$(".username").text(data);

Comments

1

You should set the html in callback

function getUsername() {
    $.ajax({
        type: 'POST',
        url: myBaseUrl + 'Profiles/ajax_getUsername',
        dataType: 'json',
        data: {

        },
        success: function(data){
            $('.username').html(data);
        }
    })
}

2 Comments

Nothing happens :S ive even tried $('.username').html('hello world') but with no result
@MarcRasmussen Because you need to execute that function first or put that code without a function.
1

Add a return statement for the function getUsername

       var result = "";
        $('.username').html(getUsername()); 
        function getUsername(){
        $.ajax({
            type: 'POST',
            url: myBaseUrl + 'Profiles/ajax_getUsername',
            dataType: 'json',
            data: {

            },
            success: function(data){
                document.write(data);
                result = data;
            }
        })
          return result;
    }

1 Comment

This won't work due to the request being asynchronous. Nothing will ever be returned.
1

You can use .load()
Api docs: http://api.jquery.com/load/
In your case:

$('.username').load(myBaseUrl + 'Profiles/ajax_getUsername',
                    {param1: value1, param2: value2});

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.