3

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

1 Answer 1

4

Try adding an exit after you echo the json so you don't also return the html below to your ajax call.

<?
if (isset($_POST['val'])) {
        header("Content-type: application/json; charset=utf-8");
        echo json_encode(array("The first string", "first"));
        exit; // add exit here so html below is not also returned
    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Damn, you are a genius!)) I was trying to solve it for about two days, researching and shrinking stuff... And the answer is so simple)) Thanks a lot man)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.