Article 6DC7F AJAX/PHP - Cannot get 'json' info to return properly.

AJAX/PHP - Cannot get 'json' info to return properly.

by
pizzipie
from LinuxQuestions.org on (#6DC7F)
Hi,
My PHP code will not send 'json' data back to caller.

My System: PHP 7.4.3-4ubuntu2.19 (cli) (built: Jun 27 2023 15:49:59)
Firefox 115.0.2(64bit)
Ubuntu 20.04

The echoed return: ["Crocodile2.JPG","FireHydrant.JPG","FlowerForPolly.JPG","JustFlowers.JPG","LeafySeaHorse1.JPG","Leaf ySeaHorse2.JPG","OurBuickDallas.JPG"].

If dataType is changed to "text" then request.done() is triggered ok.

Thanks in advance for help in this,

R

JAVASCRIPT
Code:
var request=$.ajax({
url: "exiftools_php/exec_exiftools_v0.1.php",
data: { ajaxArr, "srcdir": "<?php echo $imageSrcDir; ?>" , "targetdir": "<?php echo $imageTargetDir; ?>"},
type: "POST",
dataType : "json"
})

request.done(function (mydata) { // fails - get nothing blank not even errors or Object messages
alert("done");
alert(mydata[0]);
})

request.fail(function (mydata) { // fails every run

alert("fail"); // alert box shows - "fail"
alert(status); // doesn't show - means alert box doesn't show
alert(errorThrown); // doesn't show - means alert box doesn't show
})

request.always(function (mydata) { // doesn't show - means alert box doesn't show
alert("always");
alert(status);
alert(errorThrown);
alert(data);

})PHP
Code:
set_include_path( '/home/rick/DBases/Dbmysql/include/');
error_reporting(E_ALL);
ini_set("display_errors", 1); // true
ini_set('html_errors', 'On');

require_once "myPhpFunctions.inc";
require_once "exifPhpFunctions_v0.inc";

myprint($_POST); // my version of print_r()

$source=$_POST['srcdir'];
$target=$_POST['targetdir'];

echo json_encode($_POST['ajaxArr']);

bye(19); // my version of exit()OUTPUT from PHP

Array // $_POST
(
[ajaxArr] => Array
(
[0] => Crocodile2.JPG
[1] => FireHydrant.JPG
[2] => FlowerForPolly.JPG
[3] => JustFlowers.JPG
[4] => LeafySeaHorse1.JPG
[5] => LeafySeaHorse2.JPG
[6] => OurBuickDallas.JPG
)

[srcdir] => /home/rick/Desktop/imageSource/A52_Test
[targetdir] => /home/rick/Desktop/imageTarget
)

ECHO - echo json_encode($_POST['ajaxArr']);

["Crocodile2.JPG","FireHydrant.JPG","FlowerForPolly.JPG","JustFlowers.JPG","LeafySeaHorse1.JPG","Leaf ySeaHorse2.JPG","OurBuickDallas.JPG"]

This goes back to 'client' and request.done(mydata) fails.

If I make only one change - dataType="text" in the ajax call, this is the result:

<pre>Array
(
[ajaxArr] => Array
(
[0] => Crocodile2.JPG
[1] => FireHydrant.JPG
[2] => FlowerForPolly.JPG
[3] => JustFlowers.JPG
[4] => LeafySeaHorse1.JPG
[5] => LeafySeaHorse2.JPG
[6] => OurBuickDallas.JPG
)

[srcdir] => /home/rick/Desktop/imageSource/A52_Test
[targetdir] => /home/rick/Desktop/imageTarget
)
["Crocodile2.JPG","FireHydrant.JPG","FlowerForPolly.JPG","JustFlowers.JPG","LeafySeaHorse1.JPG","Leaf ySeaHorse2.JPG","OurBuickDallas.JPG"]

Bye from exec_exiftools_v0.1.php Line 19 // my version of exit()
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments