Reputation: 6404
I'm facing a problem with an ajax request. whenever I run the following I am getting an x
(as what is being generated from the error function in the request below) but I should be getting the tick image since I confirmed that the call did actually make the change in the system.
Can anyone help me figure out what I'm doing wrong?
Ajax Call:
$.ajax({
url: "/URL",
type: "POST",
dataType: 'json',
data: { action: 'assoc_users', username: 'testuser', name: 'testname' },
success: function(){
$("#user_1_image").attr("src","http://domain.com/check.png");
},
error: function(){
$("#user1_status").html("x");
}
});
Data that will be the output:
{status: 'OK', payload: ''}
Upvotes: 0
Views: 3203
Reputation: 5747
You have a Parse error in your json string, check your json string first
Parse error on line 1:
{ action: 'assoc_users
-----^
Expecting 'STRING', '}'
I've corrected your json string and tried it out. Try this it works,
$(document).ready(function() {
var actions = { "action": "assoc_users", "username": "testuser", "name": "testname" };
$.ajax({
url: "/URL",
type: "POST",
data: actions,
dataType: "json",
success: function(data){
alert('action'+data.action+'username'+data.username+'name'+data.name); // this is to check whats coming from the server side
$("#user_1_image").attr("src","http://domain.com/check.png");
},
error: function(jqXHR, exception){
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
});
in the php file
<?php echo json_encode($_POST); ?>
Upvotes: 1
Reputation: 74748
if you are getting valid json from your specified url then this will work because in your code you are not passing data in the success function:
$.ajax({
url: "/URL",
type: "POST",
dataType:'json',
data: { action: 'assoc_users', username: 'testuser', name: 'testname' },
success: function(data){
if(data){
$("#user_1_image").attr("src","http://domain.com/check.png");
}
},
error: function(){
$("#user1_status").html("x");
}
});
Upvotes: 1
Reputation: 4591
kindly use it , MAy be quotes are creating problem , so try it .
$.ajax({
url: '/URL',
type: 'POST',
dataType: 'json',
data: { action: 'assoc_users', username: 'testuser', name: 'testname' },
success: function(){
$('#user_1_image').attr('src','http://domain.com/check.png');
},
error: function(){
$('#user1_status').html('x');
}
});
Upvotes: 0