fbh fbh - 21 days ago 5
Ajax Question

How to fix 301 Moved permanently for Ajax request

My ajax request is runnnig in a perfect way on my host but whenever the application is installed on the server all the request have the status "301 Moved permanently".

I already passed 3 days debuging this but no way. I don't know why i'm getting this. I'm using Symfony2.

This is the request on the server side:

$('#edit').click(function () {
var $validEmail = $('#mail').validator({
type: 'error',
rules: [{
control: 'email',
message: 'Format invalide'
}]
});
var $validpost_code = $('#post_code').validator({
type: 'error',
rules: [{
control: 'required',
message: 'Code postal obligatoire'
}]
});
var $validRs = $('#rs').validator({
type: 'error',
rules: [{
control: 'required',
message: 'Raison sociale obligatoire'
}]
});
var $validAddr = $('#addr').validator({
type: 'error',
rules: [{
control: 'required',
message: 'Adresse obligatoire'
}]
});
var $validPhone_number = $('#phone_number').validator({
type: 'error',
rules: [{
control: 'required',
message: 'Tel obligatoire'
}]
}) && $('#phone_number').validator({
type: 'error',
rules: [{
control: 'phone',
message: 'Format requise mask: +33 # ## ## ## ##'
}]
});

var $validCountry = $('#country').validator({
type: 'error',
rules: [{
control: 'required',
message: 'Ville obligatoire'
}]
});
var $validTown = $('#town').validator({
type: 'error',
rules: [{
control: 'required',
message: 'Pays obligatoire'
}]
});
var $valid3897 = $('#3897').validator({
type: 'error',
rules: [{
control: 'required',
message: 'Forme juridique obligatoire obligatoire'
}]
});
if ($validEmail && $validpost_code && $validRs && $validAddr && $validPhone_number && $validTown && $valid3897 && IsPosInteger($('#post_code').find('input').val())) {
var data = {
name: $('#name').find('input').val(),
id: "{{client.id}}",
post_code: $('#post_code').find('input').val(),
rs: $('#rs').find('input').val(),
address: $('#addr').find('input').val(),
email: $('#mail').find('input').val(),
effectif: $('#efectif').find('input').val(),
turnover: $('#turnover').find('input').val(),
siteWeb: $('#site').find('input').val(),
phone_number: $('#phone_number').find('input').val(),
country: $('#country').find('input').val(),
town: $('#town').find('input').val(),
fax: $('#number').find('input').val(),
kbis: $('#bis').find('input').val(),
vat: $('#vat').find('input').val(),
legal_form: $('#3897').find('select').val(),
active: $('#active').attr('checked'),
groupe: $('#groupe').find('input').val(),
business_sector: $('#sa').find('input').val()
};
$("#site_content").block();
$.ajax({
url: "{{url('Update_client')}}",
type: "POST",
dataType: 'json',
data: {
data: ODEANCE.toJson(data)
},
success: function (updateResponse) {
if (updateResponse) {
$("#site_content").unblock(),
jAlert('Modification effectuée avec sucées ', 'Modification') //,
//document.location=document.location
}
}
});
} else {
jAlert('Veuillez vérifier les formats de vos champs', 'Modification');
}
});


and here the method in the controller:

public function updateClientAction() {
try {
//Getting sended data
$data = $this->getRequest()->get('data');
$data = \json_decode(\stripslashes($data));
$spi = $this->get('spi');
$clientManager = $spi->getClientManager();
$updateResponse =($clientManager->update($data) == true ) ? 1 : 0;
return new Response($updateResponse);
} catch (\Exception $ex) {
$updateResponse = 0;
return new Response($updateResponse);
}
}


It seems that the response of the request cannot return to the origin location (of the request).

fbh fbh
Answer

After few days, the problem is fixed now. it was a conflict betwwen http and https request so all i did is forcing my application to the https side with this line is the security.yml

access_control:        
      - { path: ^/,role: ROLE_USER, requires_channel: https }

Thanks everyone ^^