troubleshooting Question

Problems writing my first POST request

Avatar of Ahmet Ekrem SABAN
Ahmet Ekrem SABANFlag for Austria asked on
JavaScriptHTMLPHPAngularJSON
40 Comments3 Solutions553 ViewsLast Modified:
Hallo!

I am working on a Web project for several months now. Since a few days ago, everything was easy, and I could send my data over the IP address with a GET request. This may not be the right way, but to me, it was functioning all well. To be honest, I do not know much about the server-side.

Now, I am requested to pass a greater amount of data to the PHP server that sends it to the database, and the IP address length exceeds 8190 characters. So, I read the Web and found out that a POST request to the PHP server would be more appropriate to pass the large amount of data.

So, I make a POST request with

	var jsonObject = json.stringify({
		 'renderedStaticPage': renderedStaticPage,
		 'newsTickerEnabled': (newsTickerEnabled ? 1 : 0)
	});
	var messageText = 'ipAddress = ' + Apostrophe
		+ strings.excerpt(ipAddress) + Apostrophe
		+ CommaSpace + Apostrophe + 'jsonObject = '
		+ Apostrophe + jsonObject + Apostrophe;

	messages.print(functionName, messageText);

	$http({
		method: 'POST',
		url: ipAddress,

		headers : {
			'Content-Type': 'application/json; charset=UTF-8'
			//'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		},
		data: JSON.stringify(jsonObject),
	}).success(function(data, status, headers, config) {
		swal('Data sent successfully.', '', 'info');
	}).error(function(data, status, headers, config) {
		swal('No connection possible!', 'URL: '
			+ messageText, 'error');
	});
};

By the way: The strings.excerpt just cuts out the beginning and the end of the content, if it exceeds 50 characters, and the messages.print just makes a compact log print to the console. So, there is nothing exciting about that…

When I make the call with the backend PHP code

<?php

include('./general/connectDB.php');
include('./general/constants.php');

$renderedStaticPage = $_POST['renderedStaticPage'];
$newsTickerEnabled = $_POST['newsTickerEnabled'];

//print "renderedStaticPage = $renderedStaticPage" . $CR;	# Test line
//print "newsTickerEnabled = $newsTickerEnabled" . $CR;	# Test line

$query = "INSERT INTO StartSite (renderedStaticPage, newsTicker_enabled) VALUES ('"
		. $renderedStaticPage . "', " . $newsTickerEnabled . ")";

// Start query
$result = MYSQL_QUERY($query);

//print "query = $query\nresult = $result" . $CR;	# Test line
	
?>

it finishes with the 'Data sent successfully' message. Unfortunately, there is no new record in the database. If I uncomment one or more of the //print lines in the PHP code, I get the error message 'No connection possible!' with the sent renderedStaticPage (should contain an HTML page or part of it) and the Boolean newsTickerEnabled (0 or 1).

I heard from a colleague today about Postman plug-in for Google Chrome that I started to use. Whenever I have uncommented one or more //print lines in the PHP code, I can see that all the variables on the server are empty, and on the client side, there is the error message

SyntaxError: Unexpected token r in JSON at position 0
    at JSON.parse (<anonymous>)
    at fromJson (ionic.bundle.js:10063)
    at defaultHttpResponseTransform (ionic.bundle.js:18080)
    at ionic.bundle.js:18171
    at forEach (ionic.bundle.js:9168)
    at transformData (ionic.bundle.js:18170)
    at transformResponse (ionic.bundle.js:18926)
    at processQueue (ionic.bundle.js:23399)
    at ionic.bundle.js:23415
    at Scope.$eval (ionic.bundle.js:24678)

if the first print starts with print "r…. In the included connectDB.php file (attached below), the header command decides to use 'application/json'. Without the JSON part, the code should be

	[…]
	var messageText = 'ipAddress = ' + Apostrophe
		+ strings.excerpt(ipAddress) + Apostrophe + CommaSpace
		+ Apostrophe + 'renderedStaticPage = ' + Apostrophe
		+ strings.excerpt(renderedStaticPage) + Apostrophe + CommaSpace
		+ 'newsTickerEnabled = ' + strings.excerpt(newsTickerEnabled);

		messages.print(functionName, messageText);
		$http({
			method: 'POST',
			url: ipAddress,

			headers : {
				'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
			},
			data: {
				'renderedStaticPage': renderedStaticPage,
				'newsTickerEnabled': (newsTickerEnabled ? 1 : 0)
			},
		}).success(function(data, status, headers, config) {
			swal('Data sent successfully.', '', 'info');
		}).error(function(data, status, headers, config) {
			swal('No connection possible!', 'URL: '
				+ messageText, 'error');
		});
		[…]

Can you help me to find out why the PHP server does not get the parameter values? If it does, I am sure the PHP code will write it to the database, as it did it before.

To eliminate other problems & for the time being, I set renderedStaticPage = '<div name="test22"></div>'.

Thank you for your help!
2017-03-07-PHP-code.zip
SOLUTION
Join our community to see this answer!
Unlock 3 Answers and 40 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 40 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros