Reputation: 6663
I have this javascript code that performs an ajax request on button click.
$('#allinea').click(function(e){
e.preventDefault();
var password = $('#password').val();
$.ajax({
type: "POST",
url: "db_align.php",
data: {password:password},
complete: function(data){
data1=data.split("|");
if(data1[0]=="Successo"){
$("#per_all").fadeTo(200,0.1,
function(){
$(this).find('img').attr("src",'../images/ok.png');
$(this).find('.right').html(data1[1]).promise().done(function(){
$(this).fadeTo(900,1);
});
});
}else if(data1[0]=="Errore"){
$("#per_all").fadeTo(200,0.1,
function(){
$(this).find('img').attr("src",'../images/alert.png');
$(this).find('.right').html(data1[1]).promise().done(function(){
$(this).fadeTo(900,1);
});
});
}
}
}); //chiudo $.ajax
});
The php page returns "Successo|Allineamento riuscito" and I see it correctly in firebug but the js returns an error:
Type error: data.split is not a function
data1=data.split("|");
I use this code everywhere in my app without any inconvenient. What am I doing wrong this time?
For what I see btw this is not an array so this should work without any inconvenient!
Upvotes: 0
Views: 2576
Reputation: 13304
Exo's answer is perfect. It will allow you to edit the raw text.
If you know the type of data returning, you can (must) set it explicitly. jQuery will then programmatically convert data
for you in the correct format.
dataType: json
for JSON,
dataType: text
for plain text
More can be found in the documentation.
You should update your
complete
method todone
for success condition andfail
for when the request fails.
complete
is deprecated as of version 1.8, it's replaced withalways
, which returns adata
orjqXHR
object. When successcomplete
will return adata
object according to the functionality ofdone
.
Upvotes: 2
Reputation: 1152
jQuery will return a jqXHR here, which means that instead of accessing data
directly, you will probably need to do data.responseText
.
Upvotes: 5