XML Parsing error for Ajax

Hey guys, I am trying to pass a JSON object from my javascript file using ajax, to my php file so i can dynamically send an email.

However, on my console I am receiving an error message:
XML Parsing Error: no root element found
Location: file:///C:/Users/tekke/Documents/My%20Websites/fulledm/sendEmail.php
Line Number 6, Column 3:

Mind you, I am doing this on my local computer without a local server running, could this be the issue?

My PHP file to test it:

<?php 
header("Content-Type: application/json; charset=UTF-8");
var_dump(json_decode($json));


?>

My JS file:
where emailContext is a regular javascript object

function prepareEmail(){
	var json = JSON.stringify(emailContext);
	$.ajax({
		type: "POST",
		url: "sendEmail.php",
		contentType: "application/json",
		data: json,
		success: function(){
			alert("we did it!");
		},
		error: function(){
			alert("failed");
		}
	});
}

Well for starters, you’re telling PHP to var_dump the decoded JSON, which has an output that is NOT itself JSON, so you’ve got a content type mismatch between the header and the code.

But yes, the problem is because you’re referencing a file://, rather than localhost via a server.

Is there anyway I can use ajax just on my computer without setting up a local server?

Perhaps you are being a bit misled by the acronym?

AJAX - Asynchronous JavaScript And XML
vs. aka.
XHR - XML HTTP Request

The clue is the “HTTP Request”

How are you running your PHP? It is possible to run PHP from the command line, but it is not something I hear others discussing very often.

Another thing that doesn’t come up in discussions often is that PHP comes with a built in server
http://php.net/manual/en/features.commandline.webserver.php

So even though it might be possible to run PHP exclusively from the command line, it would be unusual. And if you are going to use PHP to output to a browser (i.e. web pages) you will need a server other than the built in one.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.