Jump to content
  • Announcements

    • boen_robot

      Guidelines and Netiquette   03/28/2017

      Posting Problems:   Having problems posting your topic? Read through this: To join, you agree to our terms and conditions and fill out and submit a registration form. An activation email will be sent to your email adress, so you'll need to verify your account. After that the account has to be validated by one of the moderators. This will mean that it can take up to a day to be activated. A couple of things to remember to ensure approval: Don't use an email address in one of those $2 four character .com domains eg. xyds.com. These will be deleted and the domain added to the banned list. Don't use an email address that is within a domain with a bad reputation for spam. A Google search is run on every email address and email domain. Don't sign up with an email address that doesn't exist, doesn't work or requires the sender to answer a quiz before their email can get to you. Put your country and or state and city in the signup form. Blank forms will go to the botton of the "to do" list. And make sure that your email address and your country match, saying you're from Alabama and using a .ru email address is not going to get you activated. After a membership is activated the first few posts will be monitored. Posting spam or unapproved topics described in the agreement results in an immediate ban. The email provider and the IP addresses associated with the account will be banned and all posts will be deleted. These strict measures have been deemed necessary to hinder spam. Sorry for any inconvenience this causes, but it's not liable to change. If, after reading this, you still can't post and don't understand why, contact one of the Moderators listed here.   Topic Guidelines   Including the following information can expedite an accurate response from board members: Must be a Specific Problem or Question related to web design and development Include Code in Question (wrap with   for small blocks of code and for longer blocks   ) Include Code Author Include Extra Notes/Modifications/Attempts Include web link to page/file when possible Content Guidelines   You may not post, upload, link to, or email any Content that contains, promotes, gives instruction about, or provides prohibited Content. Prohibited Content includes any Content that breaks any local, state, county, national or international law. Prohibited Content also includes: No direct or indirect advertising or websites, forums, products, services No hijacking of posts (do not post your question in someone elses) Content that infringes upon any rights [ex. MP3s and ROMs] (including, but not limited to, copyrights and trademarks) Abusive, threatening, defamatory, racist, or obscene Viruses or any other harmful computer software False Information or libel Spam, chain letters, or Pyramid schemes Gambling or Illicit drugs Terrorism Hacking or cheating for internet/online games Warez, Roms, CD-Keys, Cracks, Passwords, or Serial Numbers Pornography, nudity, or sexual material of any kind Excessive profanity Invasive of privacy or impersonation of any person/entity Hacking materials or information Posting Tips   There are more BBcodes than there are buttons for on the reply menu. To get the full list, click "BB Code Help" underneath the clickable smilie face menu. Use   for small snippets of code Use   for lengthy snippets of code Use   if your snippet is HTML (optional) Use   if your snippet is SQL (optional) Rules of Conduct   Be nice. There's no need for calling someone stupid if they ask an 'easy' question. Keep your avatars and signatures absolutely child friendly. We have a younger audience on this forum. Keep your language appropriate for the same reason above. Do not PM moderators for help on the forum. Post on the topic, or create a new one.   Spam:   Recently, as you have all without doubt noticed, we have had lots of spam and advertisement on the forum. Therefore, we'd like to alert you as to what to do when you have found any of the aforementioned annoying messages: it. Immediately. Give a clear reason, please, if the advertising is not evident. DO NOT POST! Report, let the post stay as is, and we will get to it, meanwhile if you continue to post as normal in the other threads, it won't be on the top so long. Refrain from PMing the member. This won't help at all, as they are most likely spambots anyway. Thank you.       Images in signatures:   After thinking of users on dial-up, we have decided to enforce the following rules regarding signatures. Please pay heed to them. Respecting these rules is respecting the members on this forum with dial-up. Signature rules: No animated images AT ALL. No matter the amount of animation. Maximum image widthxheight: 300x150 Maximum image (file) size: 15kb Use calm colors. Do not use highly contrasting images in your signature, as this can get really annoying when seeing several posts from one member in the same thread. The same prohibited content goes for images as for posts. Lastly, use common sense. No lengthy signatures please. Save us some scrolling. Thank you.       Links in signatures:   Please understand that w3schools.com only exists because of voluntary work and is barely supported by the advertising littered throughout the tutorials and the forum. So, please, stop advertising other sites. DO not post links that drive traffic away from the w3schools domain - especially to a site that offers similar if not identical information. Please help support the site by keeping individuals on it. Thank you. Here are some guidelines as to what you can put in your signature: w3schools links --> allowed w3.org links --> allowed browser links --> allowed html editor links --> allowed personal sites --> allowed tutorial sites competing with w3schools --> NOT allowed sites completely irrelevant to webprogramming and this forum --> NOT allowed   Thanks for understanding, and for taking the time to read this. ~W3Schools Modstaff~
iwato

A Correct Way to Read via an AJAX or similar HTTP Request the Output of a PHP Routine

Recommended Posts

BACKGROUND:  I have created a PHP routine that gathers data from a MySQL database.  The data that it gathers depends  on the value of an HTTP request that uses the $_GET superglobal as its transfer mechanism.  The routine further organizes the retrieved information into a nested array such that each element of the array corresponds to a different table row, and each element of each nested array corresponds to a subset of the table fields.  Once the nested array has been completed the json_encode() function converts the array into a JSON string and readies it for transport.

The PHP

<?php
	if(isset($_GET['podType']) {
		define('_HOST_NAME','...');
		define('_DATABASE_NAME','...');
		define('_DATABASE_USER_NAME','...');
		define('_DATABASE_PASSWORD','...');
		$mysqli_obj = new MySQLi(_HOST_NAME,_DATABASE_USER_NAME,_DATABASE_PASSWORD,_DATABASE_NAME);
		if($mysqli_obj->connect_errno) {
			die("ERROR : -> ".$mysqli_obj->connect_error);
		}
		$tbl_name = 'rss2_podcast_item';
		$podcast_items = [];
		$result_obj = $mysqli_obj->query("SELECT * FROM " . $tbl_name);
		while($row = $result_obj->fetch_assoc()) {
			foreach($row as $key => $value) {
				$item_arr[$key] = $value;
			}
			$items[] = $item_arr;
		}
		//Creates a nested array whose subarrays consist of the four indicated elements.
		foreach ($items as $sub_arr => $element) {
			$podcast_item[] = $element['podcast_no_item'];
			$podcast_item[] = $element['item_title'];
			$podcast_item[] = $element['item_description'];
			$podcast_item[] = $element['item_pubdate'];
			$podcast_items[] = $podcast_item;
			$podcast_item = [];
		}
		echo json_encode($podcast_items, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK);
	}
?>

The Javascript

<script>
	$(document).ready(function() {
		$('li.podDex').each(function(){
			var podType = $(this).attr('id');
			//Causes the weight of the font to change when the mouse passes over the text.
			$('#'+podType).mouseover(function() {
					$(this).css({"cursor": "pointer", "font-weight":"800"});
				})
				.mouseout(function() {
					$(this).css("font-weight", "normal");
			});
			//Causes the weight of the font to change, displays the hidden text to appear in the #main <div> element, and brings about a change in color.
			$('#'+podType).mouseover(function() {
					$(this).css({"cursor": "pointer", "font-weight":"800"});
				})
				.click(function() {
					$.getJSON({
						url: "../exp1/ajax_data.php",
						data: podType,
						success: function(json_data){
							var data_array = $.parseJSON(json_data);
							alert(data_array[0]['podcast_no_item']);
						}
					});
				})
				.mouseup(function() {
					$(this).css({"color": "#fadb9d","font-weight": "normal"});
				$('body, html').animate({scrollTop: $('#main').offset().top},800);
			});
		});
	});
</script>

The HTML

					<ul>
						<li id='linear' class='podDex'>Linear Analysis</li>
						<li id='clausal' class='podDex'>Clausal Analysis</li>
						<li id='inversion' class='podDex'>Socratic Inversion</li>
						<li id='chronology' class='podDex'>Chronology</li>
					</ul>

The Console Error Message is

Quote

"Failed to load resource:  The server responded with a status of 404  (Not Found)

You are welcome to try it yourself.  Please go to Grammar Captive and click on any of the four designated <li> elements.

QUESTION:  What might be causing the $.getJSON() call to fail?  

NOTE:  A similar, but not identical, result occurs on my local  test server.

Share this post


Link to post
Share on other sites

404 means that the URL of the file is wrong.

Share this post


Link to post
Share on other sites

OK.

Here are the two paths:

... /experimentation/exp2/podcast_index.php

.../experimentation/exp1/ajax_data.php

The value of the URL property of the AJAX object in podcast_index.php is

url: "../exp1/ajax_data.php",

I have checked the spelling of each folder and file.  I find nothing wrong.  Any other ideas?

This is what the Javascript Console returns as part of the error message.

http://www.grammarcaptive.com/podcasting/experimentation/exp2/[object Object]

Is $.getJSON looking for a JSON object as the value of a defined variable, when indeed, I am only echoing the JSON string?  If this is the problem what form should the variable  take?  Can I use a PHP variable?  Must it be a Javascript variable?  How should I set the datatype of the $.ajax() or $.getJSON() PlainObject to correspond?

Truly, I do not know how AJAX retrieves its desired information.  Does it scan the entire page looking for a variable name?  Does it look for some sort of page output?  Or, does it simply read everything of the designated data type regardless if it is needed?  None of the literature that i have read tells what truly happens once the designated URL has been accessed.

Edited by iwato

Share this post


Link to post
Share on other sites

When you see "[object Object]", that means you're trying to print an object (or otherwise use the object in a string context).  When Javascript converts an object to a string that's what the result is.  So, wherever that URL is being built, you're trying to append an entire object to the end of it.

Share this post


Link to post
Share on other sites

Are you suggesting that the PHP echo statement produces an object?  I just replaced the following with the code that follows.

FOLLOWING CODE

echo json_encode($podcast_items, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK);

CODE THAT FOLLOWS

<?php
		...

		$json_data = json_encode($podcast_items, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK);
?>
		<script>
			var json_data = '<?php echo $json_data; ?>';
		</script>
<?php

Still no change: the alert does not appear and the error message along with the accompanying URL remain the same.

Roddy

Share this post


Link to post
Share on other sites

No, it isn't PHP, that's Javascript which does that.  When you print an object from PHP it does something else, that output is coming from Javascript.  Find the part where that URL is actually built.  If it's somewhere in the JSON structure, find it and see what's being appended to the URL and if it needs to be something else.

Share this post


Link to post
Share on other sites

This is all that I am doing.

					$.getJSON({
						url: "../exp1/ajax_data.php",
						data: podType,
						success: function(json_data){
							var data_array = $.parseJSON(json_data);
							alert(data_array[0]['podcast_no_item']);
						}
					});

I read this in simple English as, "Send an HTTP request with the value of podType to '../exp1/ajax_data.php'. Discover  a variable called json_data in ajax_data.php" .  Read the value of json_data into the success call  function for further processing.   Convert the JSON string into a JSON object and extract from it the value of the first element of the first subarray of the Javascript array just created.

This too fails.

Is this the source of the problem?  Must this be stringified before it is sent back to the requesting file?

[[60,"New Title","Test","2017-06-19 17:26:41"],[51,"The First Tri-Lateral Attempt","Having rearranged the PHP code, the MySQL database will not be updated until all mandatory elements have been completed and the submit button is depressed.","2017-06-18 09:50:37"],[52,"Cooperation","New description","2017-09-24 11:00:17"],[52,"New Title","New description","2017-06-18 09:56:36"],[53,"Experiment One","Test","2017-06-18 10:24:49"],[54,"Experiment Two","Test","2017-06-18 10:31:48"],[54,"Experiment Two","Test","2017-06-18 10:31:48"],[55,"Experiment Three","Test","2017-06-18 11:10:56"],[55,"Experiment Three","Test","2017-06-18 11:10:57"],[55,"Experiment Three","Test","2017-06-19 08:43:49"],[55,"Experiment Three","Test","2017-06-19 08:43:49"],[57,"Eureka_3","I have found it.","2017-09-24 11:03:50"],[58,"Is it ready?","It sure looks like it.","2017-06-19 15:44:59"],[58,"Is it ready?","It sure looks like it.","2017-06-19 17:21:11"],[60,"New Title","Test","2017-06-19 17:24:23"],[60,"New Title","Test","2017-06-19 17:26:41"],[60,"New Title","Test","2017-08-18 08:21:45"],[63,"Test","Test","2017-08-18 08:21:27"],[64,"Podcast #64","Practice Podcast","2017-08-18 08:21:04"],[65,"Transition Without End","There is a time for everything including a time to end.","2017-08-18 08:20:42"],[60,"New Title","Test","2017-06-19 17:26:41"]]

 

Edited by iwato

Share this post


Link to post
Share on other sites

If that function is supposed to receive a JSON string, then you shouldn't wrap the response inside script tags in PHP.  Output only the JSON data if that's what the callback function expects.  If you try to parse the script tags and variable declaration it's going to be a JSON parse error, because the script tags and variable declaration are not part of JSON.

Also, if podType is a scalar or primitive value, like a string ID, then you aren't passing it right, you need to give it a name.  Right now you're just sending a value.  I believe you can do that like this:

data: {varName: podType}

Then, in PHP, you would look up $_GET['varName'] or $_POST['varName'] (I think that getJSON sends a get request, but I might be wrong) to look up the value that was passed, in this case the value of the podType variable.  Each thing that gets passed in a get or post request needs both a name and value.

There's still a piece missing, though.  If this is the URL with the problem:

http://www.grammarcaptive.com/podcasting/experimentation/exp2/[object Object]

Then you must be building that URL somewhere, and I haven't seen the code that does that.

Share this post


Link to post
Share on other sites

1) No.  I am not building the URL,  I am simply entering it as a relative path.

2) Also, because $.getJSON() appears to be  a special case of $.ajax(), I should think that it follows the same rules for the data parameter.  This is what it says for the parameter for $.ajax(). 

Quote
  • data
    Type: PlainObject or String or Array
    Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

So, I believe you are correct about the reformatting of the value that I enter for the data parameter. 

3) You wrote:

Quote

If that function is supposed to receive a JSON string, then you shouldn't wrap the response inside script tags in PHP.  Output only the JSON data if that's what the callback function expects.

Yes, but how to I output the JSON string.  Originally, I did it as a PHP echo statement. 

I understand far too poorly what $.ajax() does when the dataType is 'JSON'.  I changed the data parameter to read 

					$.getJSON({
						url: '../exp1/ajax_data.php',
						data: {'podType': podType},
						success: function(jsonData){
							var data_array = $.parseJSON(jsonData);
							alert(data_array[0]['podcast_no_item']);
						}
					});

and the data file to read

<?php
	if(isset($_GET['podType']) {
		define('_HOST_NAME','baha.lunarbreeze.com');
		define('_DATABASE_NAME','thege0_grammarcaptive');
		define('_DATABASE_USER_NAME','thege0');
		define('_DATABASE_PASSWORD','Liberty2015');
		$mysqli_obj = new MySQLi(_HOST_NAME,_DATABASE_USER_NAME,_DATABASE_PASSWORD,_DATABASE_NAME);
		if($mysqli_obj->connect_errno) {
			die("ERROR : -> ".$mysqli_obj->connect_error);
		}
		$tbl_name = 'rss2_podcast_item';
		$podcast_items = [];
		$result_obj = $mysqli_obj->query("SELECT * FROM " . $tbl_name);
		while($row = $result_obj->fetch_assoc()) {
			foreach($row as $key => $value) {
				$item_arr[$key] = $value;
			}
			$items[] = $item_arr;
		}
		//Creates a nested array whose subarrays consist of the four indicated elements.
		foreach ($items as $sub_arr => $element) {
			$podcast_item[] = $element['podcast_no_item'];
			$podcast_item[] = $element['item_title'];
			$podcast_item[] = $element['item_description'];
			$podcast_item[] = $element['item_pubdate'];
			$podcast_items[] = $podcast_item;
			$podcast_item = [];
		}
		$json_data = json_encode($podcast_items, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK);
	}
?>
<script>
	var jsonData = '<?php echo $json_data; ?>';
	jsonData = JSON.stringify(jsonData);
</script>
  1.  

Edited by iwato

Share this post


Link to post
Share on other sites

I also tried this

<?php
...
?>
<script>
	(function jsonOutput() {
		var jsonData = '<?php echo $json_data; ?>';
		jsonData = JSON.stringify(jsonData);
		return jsonData;
	})()
</script>

but still, the error message and associated link remain unchanged.

Edited by iwato

Share this post


Link to post
Share on other sites

Yes, I have cleared the cache.

You are welcome to try it at http://www.grammarcaptive.com/podcasting/experimentation/exp2/podcast_index.php

Find the <div> entitled Navigation Bar and click on any of the menu items.  Each will send a different value for podType, but the if condition that controls the PHP in the data file does not care what it receive, just so long as it is something.

Roddy

Edited by iwato

Share this post


Link to post
Share on other sites

I even found a missing parenthesis in the condition expression of the if-statement and fixed that.  Still, the error message remains the same.

 

 

Share this post


Link to post
Share on other sites

Although I am happy to report that I have managed to rid myself of the Object: object and am likely now in communication with the data file, I am still unable to achieve the desired result -- namely, the Javascript alert ( ) and more positive feedback from the introduction of the  console.log( ) method.

I believe that there were several coding mistakes that were bringing about the original error message including a missing parenthesis in the condition of the data file's if-statement and my treatment of the $.getJSON arguments.   I have switched to the $.ajax( ) method, as its format provides me with greater flexibility and a clearer picture of what I am actually trying to achieve.  Further, I have move the Javascript outside of the PHP routine in the requested data file.

In addition, I have checked to insure that the desired JSON object actually exists and  have displayed the result below.  The check was made by turning off the $_GET request and running a simple echo statement of the result.

In addition, please find below the PHP file that generates the JSON string and the AJAX triggered by a click function using the $.ready( ) method.

<?php
	ini_set('display_errors', 1);
	ini_set('display_startup_errors', 1);
	error_reporting(E_ALL);
	if(isset($_GET['podType'])) {
		define('_HOST_NAME','...');
		define('_DATABASE_NAME','...');
		define('_DATABASE_USER_NAME','...');
		define('_DATABASE_PASSWORD','...');
		$mysqli_obj = new MySQLi(_HOST_NAME,_DATABASE_USER_NAME,_DATABASE_PASSWORD,_DATABASE_NAME);
		if($mysqli_obj->connect_errno) {
			die("ERROR : -> ".$mysqli_obj->connect_error);
		}
		$tbl_name = 'rss2_podcast_item';
		$podcast_items = [];
		$result_obj = $mysqli_obj->query("SELECT * FROM " . $tbl_name);
		while($row = $result_obj->fetch_assoc()) {
			foreach($row as $key => $value) {
				$item_arr[$key] = $value;
			}
			$items[] = $item_arr;
		}
		//Creates a nested array whose subarrays consist of the four indicated elements.
		foreach ($items as $sub_arr => $element) {
			$podcast_item[] = $element['podcast_no_item'];
			$podcast_item[] = $element['item_title'];
			$podcast_item[] = $element['item_description'];
			$podcast_item[] = $element['item_pubdate'];
			$podcast_items[] = $podcast_item;
			$podcast_item = [];
		}
		$json_data = json_encode($podcast_items, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK);
	}
?>
<script>
	(function jsonOutput() {
		var jsonData = '<?php echo $json_data; ?>';
		jsonData = JSON.stringify(jsonData);
		return jsonData;
	})()
</script>
				.click(function() {
					$.ajax({
						url: '../exp1/ajax_data.php',
						dataType: 'JSON',
						data: {'podType': podType},
						success: function(jsonData){
							var data_array = $.parseJSON(jsonData);
							document.console && console.log(data_array);
							alert(data_array[0]['podcast_no_item']);
						}
					});
				})
[
	[60,"New Title","Test","2017-06-19 17:26:41"],
	[51,"The First Tri-Lateral Attempt","Having rearranged the PHP code, the MySQL database will not be updated until all mandatory elements have been completed and the submit button is depressed.","2017-06-18 09:50:37"],
	[52,"Cooperation","New description","2017-09-24 11:00:17"],
	[52,"New Title","New description","2017-06-18 09:56:36"],
	[53,"Experiment One","Test","2017-06-18 10:24:49"],
	[54,"Experiment Two","Test","2017-06-18 10:31:48"],
	[54,"Experiment Two","Test","2017-06-18 10:31:48"],
	[55,"Experiment Three","Test","2017-06-18 11:10:56"],
	[55,"Experiment Three","Test","2017-06-18 11:10:57"],
	[55,"Experiment Three","Test","2017-06-19 08:43:49"],
	[55,"Experiment Three","Test","2017-06-19 08:43:49"],
	[57,"Eureka_3","I have found it.","2017-09-24 11:03:50"],
	[58,"Is it ready?","It sure looks like it.","2017-06-19 15:44:59"],
	[58,"Is it ready?","It sure looks like it.","2017-06-19 17:21:11"],
	[60,"New Title","Test","2017-06-19 17:24:23"],
	[60,"New Title","Test","2017-06-19 17:26:41"],
	[60,"New Title","Test","2017-08-18 08:21:45"],
	[63,"Test","Test","2017-08-18 08:21:27"],
	[64,"Podcast #64","Practice Podcast","2017-08-18 08:21:04"],
	[65,"Transition Without End","There is a time for everything including a time to end.","2017-08-18 08:20:42"],
	[60,"New Title","Test","2017-06-19 17:26:41"]
]

I look forward to a speedy remedy of my ignorance.

Finally, I have replaced the deprecated jQuery .parseJSON() function with the Javascript JSON.parse() function.

Roddy

 

Edited by iwato

Share this post


Link to post
Share on other sites

No.  I am not building the URL,  I am simply entering it as a relative path.

OK, apparently this is a communication issue.  That URL IS getting built somewhere, unless you manually typed "[object Object]" onto the end of the URL, which seems like kind of a strange thing for someone to do.  So it really, really sounds like Javascript is building that URL and trying to append an object onto the end of it.  But I'll leave it up to you to work backwards how that URL ends up on the page to figure out why that's happening.  Because my ~20 years of working with Javascript are telling me exactly what's going on, but I haven't seen the code which is actually doing it yet.  If you want to disagree with me, that's fine, but I can't help you.

Yes, but how to I output the JSON string.  Originally, I did it as a PHP echo statement.

That should be fine.  That's what echo is for.  If the Javascript expects JSON, then only output JSON.  Don't output HTML tags or other Javascript code, that's not JSON.  Right now your PHP code is outputting HTML script tags and various other Javascript code, and that's a problem if you're trying to parse JSON.  This should only be a data exchange between PHP and Javascript, all you need is the actual JSON data, nothing else.  Anything else will cause a problem.

Share this post


Link to post
Share on other sites

Good morning!

Firstly, I receive a parse error when I echo the PHP variable $json_data without the Javascript.

In contrast, I receive no error message when I use the Javascript.  I believe now that the problem is with the success function.

Find below the two key functions that together produce no error, but still do not produced the expected result -- namely, the alert and a value in the console window.

<script>
	(function jsonOutput() {
		var jsonData = '<?php echo $json_data; ?>';
		jsonData = JSON.stringify(jsonData);
		return jsonData;
	})()
</script>

and

						success: function(jsonData){
							var data_array = JSON.parse(jsonData);
							document.console && console.log(data_array);
							alert(data_array[0]['podcast_no_item']);
						}

and still again,

[
	[60,"New Title","Test","2017-06-19 17:26:41"],
	[51,"The First Tri-Lateral Attempt","Having rearranged the PHP code, the MySQL database will not be updated until all mandatory elements have been completed and the submit button is depressed.","2017-06-18 09:50:37"],
	[52,"Cooperation","New description","2017-09-24 11:00:17"],
	[52,"New Title","New description","2017-06-18 09:56:36"],
	[53,"Experiment One","Test","2017-06-18 10:24:49"],
	[54,"Experiment Two","Test","2017-06-18 10:31:48"],
	[54,"Experiment Two","Test","2017-06-18 10:31:48"],
	[55,"Experiment Three","Test","2017-06-18 11:10:56"],
	[55,"Experiment Three","Test","2017-06-18 11:10:57"],
	[55,"Experiment Three","Test","2017-06-19 08:43:49"],
	[55,"Experiment Three","Test","2017-06-19 08:43:49"],
	[57,"Eureka_3","I have found it.","2017-09-24 11:03:50"],
	[58,"Is it ready?","It sure looks like it.","2017-06-19 15:44:59"],
	[58,"Is it ready?","It sure looks like it.","2017-06-19 17:21:11"],
	[60,"New Title","Test","2017-06-19 17:24:23"],
	[60,"New Title","Test","2017-06-19 17:26:41"],
	[60,"New Title","Test","2017-08-18 08:21:45"],
	[63,"Test","Test","2017-08-18 08:21:27"],
	[64,"Podcast #64","Practice Podcast","2017-08-18 08:21:04"],
	[65,"Transition Without End","There is a time for everything including a time to end.","2017-08-18 08:20:42"],
	[60,"New Title","Test","2017-06-19 17:26:41"]
]

And, still again, if there is doubt or a need for further experimentation.

You are welcome to try it at http://www.grammarcaptive.com/podcasting/experimentation/exp2/podcast_index.php

Edited by iwato

Share this post


Link to post
Share on other sites

Firstly, I receive a parse error when I echo the PHP variable $json_data without the Javascript.

In contrast, I receive no error message when I use the Javascript.  I believe now that the problem is with the success function.

Well, you're wrong.  If you're going to use it as JSON, then it needs to be JSON, that's about as simple as it gets.  If you want to fix the parse error, great, we can do that if we see what data you're trying to parse, and what the error message is.  But let's not make incorrect assumptions about what the problem is.  There's no reason to assume anything, everything can be tested.

 

Quote

Find below the two key functions that together produce no error, but still do not produced the expected result -- namely, the alert and a value in the console window.

 

I know exactly why it's not producing the expected result, because you're trying to pass a bunch of HTML tags and Javascript code to a JSON parser.  That's the reason.  But, like I said, there is no reason to make assumptions.  Add some debugging code to your success function and you'll see exactly what it's trying to do:

 

success: function(jsonData){
  console.log('Success callback');
  console.log(jsonData);
  var data_array = JSON.parse(jsonData);
  console.log(data_array);
  alert(data_array[0]['podcast_no_item']);
}

 

When I run your page right now, your success function doesn't even get called, and there's a PHP error message in the response.  I can put a breakpoint on the ajax call to verify that it sends the ajax request, but if I put a breakpoint inside the success function it never gets hit, that function never gets called.  I assume that's because jQuery tries to parse the response as JSON and fails, since it is not JSON, and thus it never calls success.  The ajax method will also accept functions for error which it will call if the request failed, and there's another one called complete that it calls after either success or error.  Right now your success function isn't being called at all, and I assume it's because of the bad data.

Share this post


Link to post
Share on other sites

The problem has been resolved by echoing a JSON string produced with json_encode( )  and eliminating the JSON.parse function in the success function.  Apparently the new version of $.ajax( ) parses incoming data strings automatically.

Try it and see:  You are welcome to try it at http://www.grammarcaptive.com/podcasting/experimentation/exp2/podcast_index.php

Share this post


Link to post
Share on other sites

That looks better, yes.  You're only sending JSON data, and like you've learned when you specify to jQuery that the data type is JSON, it will automatically parse it and fire the success function only if that worked.  If you did not specify a data type of JSON then it would return the response as a string.

You can also build that array in PHP so that it is an associative array with string names for the keys instead of numbers, and in Javascript that can be decoded as an object with named properties.  So you could have data_array[0].id, data_array[0].title, etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×