AJAX / PHP working on localhost bu not working on server - php

I am trying to make the button post data to another PHP file through AJAX. When I run the code on XAMPP it returns the data in a dialog box perfectly fine. However, when I run it on the server, it returns the current page's HTML structure.
AJAX CODE:
$.ajax({
type: "POST",
url: "post.php",
data: {user_1: 'hello', user_2: 'hey'},
success: function(data) {
alert(data);
}
});
SERVER ALERT RESPONSE SCREENSHOT

Related

passing string to opencart using ajax

I have an Opencart website, I am currently trying to use ajax at the frontend to pass data to php controller in the backend, but I am unable to get the value from the request in backend
here is the frontend ajax code:
$.ajax({ url: 'index.php?route=checkout/cart/addAll',
type: 'post',
data: 'product_list= test' ,
dataType: 'json',
success: function(json) {});
at the backend controller, I am trying to retrieve variable "product_list" but it is not working
$products = $this->request->post['product_list'];
$logger->write("products to add to cart is"+ strval($products));
the last statement keeps printing 0 to the log file
any help with this ? what is wrong here?
I also tried
$products = json_decode($this->request->post['product_list'], true);
with same results
Ok, fixed, Ajax was not the issue, it was accessing the variable from server side, so I used $_POST
instead of $this->request->post and it is working fine
So I just did this
in file catalog/view/theme/defaulttemplate/common/home.twig I add this code at the end of the file
$(document).ready(function() {
$.ajax({url:'index.php?route=checkout/cart/addAll',
type: 'post',
data: 'product_list= test' ,
dataType: 'json',
success: function(json) {}
});
});
and in file catalog/controller/checkout/cart.php on line 479 I add this
public function addAll(){
print_r($this->request->post);
}
And I see this in my console http://joxi.ru/krDlvPdfKGejar
All I did was correct your js code. hope this helps.

JSON appearing as PHP array without using json_decode()

I'm using jstree on a project and attempting to save my tree to a database.
I'm obtaining the tree data as follows:
var tmp = $('#tree').jstree(true).get_json();
console.log(tmp);
This produces a JSON object in the console as I'd expect:
However when I post this to a PHP script using jquery...
$.ajax({
type: 'POST',
url: '/saveTree',
data: {'tree': tmp},
success: function(msg) {
console.log(msg);
}
});
... It is showing a PHP array of my data:
The script which I have at /saveTree displays the POST data in the tree array post key:
var_dump($this->request->data['tree']);
I assumed since the data I'm posting to the script is in JSON format I'd need to json_decode() it at the other end? If not, why not?
I've also tried adding dataType: 'json', in the ajax request but that makes no difference.
What's happening here?
Please note the PHP script at /saveTree is running in CakePHP 2.x so the line of PHP above is equivalent to var_dump($_POST['tree']) in regular PHP.
If you want send the data as string you can JSON.stringify(tmp);
tmp = JSON.stringify(tmp);
$.ajax({
type: 'POST',
url: '/saveTree',
data: {'tree': tmp},
success: function(msg) {
console.log(msg);
}
});

How to see variable that is echoed from php file - jquery ajax

I'm trying to get echoed variable from php and display on the console. I'm using jquery to send data and I know the backend php file script is working fine. Since I want to see the progress and the backend php script echo out the iteration number while the script is running, I'm trying to see how I can get the echoed variable with jquery and display on the console.
I've tried success, onloading but it doesn't seem to work. Any help would be very appreciated. Thank you!
EDIT
$.ajax({
url: location,
type: 'POST',
data:{
iteration:"10",
readsize:"200",
slimdepth:"3",
sourcedirectory:source,
packagepath:MP
},
dataType:'json',
success: function (response) {
console.log("SUCCESS!");
console.log(response);
},
onInteractive:function (response) {
console.log(response.responseText);
},
complete:function (response) {
console.log(response.responseText);
},
error: function (response) {
console.log("Failure!");
console.log(response.responseText);
}
});
So this is how I send the data and backend php script gets the parameters without problems and it runs. Now I can't see anything until php is done running and then it echo everything. In the backend php script, I have iteration number that I want to check and that is the problem since I can't check while it is running but only it is done running.
From what I could understand from your question probably this is what you are looking for.
You are sending data to PHP file with jQuery post or send:
$.post(file.php, {post}, function(data){
});
If you want to log returned data from php in console you must use console.log() function:
$.post(file, {post}, function(data){
console.log(data)
});
and now data will be logged into browser console.

net::ERR_EMPTY_RESPONSE when post with ajax

Hello im trying to upload a xlsx file one by one so i can show a status bar, the problem is, i did it with a for loop and while loop sending a request via ajax, but when is on the 40th element it stops and the console shows POST (site.php) net::ERR_EMPTY_RESPONSE, i tried to do it in the localhost and it works perfect, but when a i try to do it on my external server(godaddy) it shows the error. here is the code.
for(j=1;j<=tama;j++){
$.ajax({
type: "POST",
url: "ejphp.php",
dataType: "json",
data: {vals: regs, j:j},
success: function(datos){
console.log(j)
prog=datos['progreso_r'];
var id_vac=datos['id_vac']
var tipo=datos['tipo']
var tipo2=datos['tipo2']
var tot_ing=datos['tot_ing']
prog_p=Math.round(prog*100/tama);
$("#progressbar").val(prog_p);
$("#progreso").text(prog_p+'%');
$("#datos_vac").text('Id Vacuno: '+id_vac);
if(prog_p==100){
$("#aceptar").show("slow");
}
if(tipo=='error') registro(tipo2, id_vac)
subir(parseInt(prog)+1);
return false;
}
})
}
Maybe your server can't pass more than 40 requests, try to increase php memory limit and execution time. And try to browse in godaddy's admin panel.

How can I implement a jquery ajax form which requests information from a web api via a php request?

I'm trying to implement a simple api request to the SEOmoz linkscape api. It works if I only use the php file but I want to do an ajax request using jquery to make it faster and more user friendly. Here is the javascript code I'm trying to use:
$(document).ready(function(){
$('#submit').click(function() {
var url=$('#url').val();
$.ajax({
type: "POST",
url: "api_sample.php",
data: url,
cache: false,
success: function(html){
$("#results").append(html);
}
});
});
});
And here is the part of the php file where it takes the value:
$objectURL = $_POST['url'];
I've been working on it all day and can't seem to find the answer... I know the php code works as it returns a valid json object and displays correctly when I do it that way. I just can't get the ajax to show anything at all!!
...data: 'url='+encodeURIComponent(url),

Categories