Why Does My AJAX Response Contain HTML Source? - javascript

hope you're all doing well. Here's the deal. I make an AJAX call to PHP and PHP decodes the JSON string then echoes a property from the json object but in the AJAX response alert, I get the correct value from the json property AND the source of the current page, for example:
jsonProperty<!DOCTYPE HTML>
<html>
<head>... [the rest of the page's source]
Here is my code:
PHP
<?php
private function validate_review(){
$json = json_decode($_POST['data']);
echo $json->review;
}
?>
AJAX:
<script>
var reviewData = {
title : $('#fieldtitle').val(),
raiting : starRaiting,
review : $('#fieldreview').val()
}
$.ajax({
type: 'post',
url: 'http://localhost/codeigniter/new-review',
data: {data: JSON.stringify(reviewData)},
success: function(result){
alert(result);
}
});
</script>
Why would the response also include the source of the page, this is completely counter-intuitive and strange. Help?

Request:
specify your request dataType
$.ajax({
type: 'post',
url: 'http://localhost/codeigniter/new-review',
data: {data: JSON.stringify(reviewData)},
dataType: 'jsonp', //tell the server that you expect json
success: function(result){
alert(result);
}
});
Response:
<?php
//DO NOT echo any output before header
header('Content-Type: application/json'); //said this response content is json
?>
and die instead of echo json content

I think your code still includes the template.
private function validate_review(){
$json = json_decode($_POST['data']);
die($json->review);
}
This should work.

Related

I can not to get the POST value from ajax

I would like to get the value from ajax with post in php, but this is not working, here is my code:
<script type="text/javascript">
function deletarPerson(id_person) {
let pag = "<?php echo plugin_dir_url(__DIR__) . 'admin/'; ?>";
jQuery.ajax({
url: pag + "/config.php",
method: "post",
data: {
id_person: id_person
},
dataType: "html",
success: function(result) {
<?php
$aux = $_POST['id_person'];
?>
alert('<?php echo $aux ?>')
},
})
}
</script>
Someone can help me?
Your Javascript ajax code:
function deletarPerson(id_person) {
jQuery.ajax({
url: "/echo.php", // <-- sending to echo.php on the server
// ...use whatever PHP file, on the server,
// you need to get the necessary information
method: "post",
data: {
id_person: id_person
},
dataType: "text",
success: function(result) {
alert(result) // <-- now use Javascript and the result variable,
// which is text sent back from the server,
// and do what you need to with it on the client
},
})
}
...and your PHP file:
<!-- echo.php -->
<?php
$aux = $_POST['id_person'];
echo $aux; // <-- whatever is echo'ed or print'ed in this file
// gets sent back to the server as text
// and is loaded in the Javascript result variable
// OR you can actually get some useful information from the server
// and send that back instead
?>
Read more about the distinct executions contexts between PHP on the server and Javascript/HTML/CSS... on the client.

AJAX request is sent, but $_POST doesn't update

The AJAX request goes through correctly, I checked with chrome's developer tools, there is a request on quiz.php page, but when I check for $_POST['risultato'] it looks doesn't exist. I noticed though that in Chrome's dev tools there's 2 quiz.php elements (one xhr the other document)
I tried changing the code in several ways, but it seems like it doesn't work
<?php
if(isset($_POST['risultato'])){
print($_POST['risultato']);
}
?>
<script>
function inviaRisultati(ris){
$.ajax({
url: "quiz.php",
type: "POST",
cache: false,
data: {risultato: ris},
success: function(){
alert("INVIATI");
}
})
}
The program is expected to return the result on quiz.php page (the same page where ajax request is fired), and it's supposed to print it somewhere
EDIT: I fixed it
<?php
file_get_contents('php://input');
if(isset($_POST['risultato'])){
print($_POST['risultato']);
}
?>
function inviaRisultati(param) {
return $.ajax({
url:"quiz.php",
method:"POST",
data:{action: "SLC", risultato :param},
dataType:"text"
});
}
inviaRisultati(1).done(function(response){``
document.open();
document.write(response);
});
In Ajax, the data attribute is in JSON format.
your data attribute will be like this
data: {risultato: ris}
Hi you can do it this way:
your php script:
if (isset($_POST["action"])) {
$action = $_POST["action"];
switch ($action) {
case 'SLC':
if (isset($_POST["risultato"])) {
$response = $_POST["risultato"];
echo $response;
}
}
break;
}
Where action is a command you want to do SLC, UPD, DEL etc and risultato is a parameter
then in your ajax:
var param = $('#yourinputid').val();
function getInfo(param) {
return $.ajax({
url:"quiz.php",
method:"POST",
data:{action: "SLC", risultato :param},
dataType:"text"
});
}
call it like this:
getInfo(param).done(function(response){
alert(response);
//do something with your response here
})
Hope it helps
HTML CODE
<script>
jQuery(document).ready(function(){
ris = 'abcd';
jQuery.ajax({
url: "quiz.php",
type: "POST",
cache: false,
data: {risultato: ris},
success: function(){
}
})
});
</script>
PHP CODE
<?php
file_get_contents('php://input');
print($_POST['risultato']);
Console Output

Can't get JavaScript result using ajax script

I am calling php file with ajax script.but when I am trying to call JavaScript in called php file it's not giving me JavaScript result in response. It's give me
Full script stored in variable.
index.php
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
//Here i am print response
}
});
response.php
$return = $_POST;
$return["js"] = "<script>test();</script>";
$return["json"] = json_encode($return);
echo json_encode($return);
?>
<script>
function test(){
return 'Hello';
}
Can any one suggest me. Thanks in advance

jQuery.ajax post is being sent but $_POST is empty

I am trying to send information using AJAX from JavaScript in one file to PHP in another file. When I inspect the page on chrome after I have pressed the button, it shows the values under the form data sub section in networks, however, if I var_dump[$_POST] it shows it is empty.
Here is my JavaScript:
function details(id) {
var data = {"id" : id};
jQuery.ajax({
url: "/Example/includes/detail.php",
type: 'POST',
data: data,
success: function(data){
jQuery('body').append(data);
},
error: function(){
alert("Error");
}
});
}
Here is the PHP:
<?php
$id = $_POST['id'];
$id = (int)$id;
?>
Any help would be greatly appreciated. Thanks

I can't passing variable to PHP from JavaScript and jQuery load the response

I want passing 2 parameters to PHP page via AJAX and load the response, but this code is not working.
JavaScript:
$(".show_category").click(function(){
var category_id = $(this).attr('data-category');
$.ajax({
url: "conx.php",
method: "POST",
data: {
action: "sort_category",
category_id: category_id
},
success: function(data) {
$("#con").load("conx.php");
}
});
});
PHP:
<?php
echo "1".$_POST["action"]."<br/>";
?>
You issue is here:
success: function(data) {
$("#con").load("conx.php");
}
At this point, you have already posted the data and received the HTML response. There is no need to make another HTTML request by calling .load, and doing so will mean requesting it again without the POST data, so it will not have the intended effect. Just use the HTML you already have in the data argument.
success: function(data) {
$("#con").html(data);
}
On a side note, this PHP code is a reflected XSS vulnerability:
<?php
echo "1".$_POST["action"]."<br/>";
?>

Categories