LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2023, 02:59 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
AJAX/PHP - Cannot get 'json' info to return properly.


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()
 
Old 07-29-2023, 12:57 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
https://www.linuxquestions.org/quest...gs-4175464257/
 
Old 07-29-2023, 02:57 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
NevemTeve, Thanks for your reply.

If regards your hint:

Quote:
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
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"  //  If dataType is "text" request.done() is OK.
})
I cannot get alert() to show a status of the Ajax return. So I don't know what to try to fix this.
 
Old 07-29-2023, 04:15 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Please edit your original post and add the missing [code] and [/code] tags.
 
Old 07-30-2023, 05:23 PM   #5
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
PHP
Code:
echo json_encode($_POST['ajaxArr']);

OUTPUT from PHP
Quote:
["Crocodile2.JPG","FireHydrant.JPG","FlowerForPolly.JPG","JustFlowers.JPG","LeafySeaHorse1.JPG","Leaf ySeaHorse2.JPG","OurBuickDallas.JPG"]
 
Old 07-31-2023, 07:38 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

The default output type in PHP is text/html. Change it before outputting json:

Code:
header('Content-Type: application/json; charset=utf-8');
echo json_encode(...);

Last edited by Guttorm; 07-31-2023 at 07:39 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP/AJAX - Any way to Upload or transfer files from Ajax to folder pizzipie Programming 4 07-19-2023 08:55 AM
Retrieve the content of a json file using shell script .Without jq /json Historia Linux - Newbie 12 03-25-2020 09:50 AM
LXer: JSON Lines: record-style JSON LXer Syndicated Linux News 0 01-28-2020 08:03 PM
Reading AJAX/JSON Responses in Linux (Bash/Perl/PHP) msf004 Programming 5 05-16-2012 03:35 AM
LXer: How to bridge the gap between XML and Ajax with JSON LXer Syndicated Linux News 0 09-14-2006 08:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration