AJAX Not Working For PHP Requests


#1

I’m trying to send POST requests to a PHP file through JQuery’s AJAX. Passing the data part of the request causes it to fail, returning a 400 error, but removing the data part causes it to succeed… kind of. I have this AJAX request in JavaScript:

        $.ajax({
    		url: "connect.php",
    		type: "post",
    		data: {
    			"action": "addMessage",
    			"username": username,
    			"message": imgData
    		},
    		dataType: 'text',
    		success: function(data, status, xhr) {
    			console.log("Success!");
    		},
    		error: function(jqXHR, textStatus, errorThrown) {
    			console.error(textStatus, errorThrown);
    		}
    	});

And all that’s in connect.php is “<?php echo "test"; ?>”. So it should display “test” right? Instead, with the data parameter, it gives a 400 error and without it, it “succeeds” but doesn’t display “test”. Any reason why AJAX doesn’t work? I know it worked a few months ago while working on another project.

Side note: running the exact same thing on a local web server with PHP works as expected, it’s just on 000webhost that it doesn’t work.


#2

Can you post your connect.php code here


#3
<?php
    echo "HELPRFDFDFDFD";
?>

Even if I replace the string with a $_POST[‘username’] to make more sense, it still gives a 400 error.

EDIT: Just tried with XMLHttpRequest and everything works, just not for uploading base64 URLs. Hmmmm.

EDIT 2: Aha, I encoded the base64 URL and now it works with XMLHttpRequest!