troubleshooting Question

Problems writing my first POST request

Avatar of Ahmet Ekrem SABAN
Ahmet Ekrem SABANFlag for Austria asked on
HTMLPHPAngular* ipJavaScript
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');
	});
};

Open in new window


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
	
?>

Open in new window


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)

Open in new window


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');
		});
		[…]

Open in new window


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
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 3 Answers and 40 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 3 Answers and 40 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004