I have a large file, but while I was debugging the problem, I have shrunk it down, to eliminate everything external. Basically I have ended up with this .php file:
<?php
if (isset($_POST['val'])) {
header("Content-type: application/json; charset=utf-8");
echo json_encode(array("The first string", "first"));
}
?>
<script type="text/javascript" src="javascript/jquery.js"></script>
<script type="text/javascript">
$.ajax({
type: 'POST',
url: 'other',
data: {val: 'text'},
contentType: "application/json",
dataType: 'json',
success: function(result) {
console.log(result);
alert(result);
},
error: function(err) {
console.log(err);
alert('Error -> ' + err);
}
});
</script>
In the original code, I have PHP and Javascript in different files, so please dont tell me to inherit PHP into javascript like this <?php ?>,
I have just researched a lot of materials, and in many people suggest this way.
In this script, an error function is triggered. When I was studding this problem, I found out, that it is triggered, when invalid json is passed.
Here is what I get in the alert box: Error -> [object Object], here is what I get in the console:
uncaught exception: out of memory <unknown>
Object { readyState: 4, getResponseHeader: .ajax/v.getResponseHeader(), getAllResponseHeaders: .ajax/v.getAllResponseHeaders(), setRequestHeader: .ajax/v.setRequestHeader(), overrideMimeType: .ajax/v.overrideMimeType(), statusCode: .ajax/v.statusCode(), abort: .ajax/v.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 11 more… }
If I change dataType to html, and remove the contentType, I get success function executed. It passes this:
["The first string","first"]
<script type="text/javascript" src="javascript/jquery.js"></script>
<script type="text/javascript">
$.ajax({
type: 'POST',
and the rest of code of the page.
I need to recieve a json. Why is this thing not working? Why don't I recieve json?
P.S. You may notice, that I don't have .php at the end of the url... I have an .htaccess file, that removes it, so that is not the problem. I was disabling it and using the script with .php, but nothing changes. The PHP code is executed, but I don't get the proper callback...