daviiies
daviiies

Reputation: 55

jQuery ajax responseText 'undefined'

I've got some jQuery which posts two variables to a php script. The PHP is very simple and simply returns a string based on what it's given, i.e. 'update successful' which I'd like to use in some way on the page.

The first time I click I get an alert saying 'undefined', any further clicks and everything works fine. Pretty sure I'm not far off but I can't work out this problem!

I've used firebug and the data is posted and the correct response is received on all attempts.

$(document).ready(function(){

    $('#updatehol').click(function() {
    additions  = $('#additions').attr('value');
    deductions   = $('#deductions').attr('value');
    datastring = 'additions='+ additions +'&deductions='+ deductions;

    $.ajax({
                type: "POST",
                data: datastring,
                url: "doadjust.php",
                complete: function(data) {
                alert(data.responseText);
                }
               });
    });
});

Upvotes: 2

Views: 30905

Answers (5)

Chuck D
Chuck D

Reputation: 1748

This is an old question but I just ran into the same problem and found the answer in the jQuery docs.

responeText and responseXml are only polulated when using dataType: text or xml. If you use any other data types it will be passed as the first param in your success callback.

From the jQuery Docs:

Data Types

The $.ajax() function relies on the server to provide information about the retrieved data. If the server reports the return data as XML, the result can be traversed using normal XML methods or jQuery's selectors. If another type is detected, such as HTML in the example above, the data is treated as text.

Different data handling can be achieved by using the dataType option. Besides plain xml, the dataType can be html, json, jsonp, script, or text.

The text and xml types return the data with no processing. The data is simply passed on to the success handler, either through the responseText or responseXML property of the jqXHR object, respectively.

Upvotes: 1

takeit
takeit

Reputation: 4081

You should add json type, ex.:

$('#test').on('click', '.testclass', function(event){
    $.post($(this).attr('href'), 
     function(data) {
        alert(data.text);
    }, 'json');
}); 

Upvotes: 0

daviiies
daviiies

Reputation: 55

Sorted it. Needed async: false option in the Ajax function.

$.ajax({
    type:     "POST",
    data:     datastring,
    url:      "doadjust.php",
    dataType: "html",
    async:    false,
    success:  function(data) {
        alert(data);
    }
});

Upvotes: 3

Syed Minhaj Uddin
Syed Minhaj Uddin

Reputation: 29

This works for me.

var min = $.ajax(
        {
            type: 'POST',
            url: 'LineChart.asmx/LineChrt',
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function(data) {
                // alert("SUCESS");
            },
            error: function(xhr, status) {
                alert("hatada:" + xhr.responseXML);
            },
            onComplete: function(data) {

            }

Upvotes: 1

alexl
alexl

Reputation: 6851

What do you get with just an alert(data) ??

btw it's success not complete:

        $.ajax({
                type: "POST",
                data: datastring,
                url: "doadjust.php",
                success: function(data) {
                   alert(data);
                }
               });
    });

Upvotes: 1

Related Questions