1

I searched and it appears that I have the opposite problem to everyone else (They wonder why ajax returns null). I WANT my jquery/ajax function to return null if PHP echoes NULL but I get empty string instead. It's not a big deal but it's something that doesn't make sense for me now.

<script>
$.ajax({
    url: "test.php",
        success:function(result){
        console.log(result);
    }
});     
</script>

<?php 

    echo NULL;

?>

//logs: empty string

I'll be embarased by the answer but I gotta know.

Thanks

2
  • 2
    echoing NULL means that the headers get sent, there are two line breaks and then the end, right? Which means you're serving an empty string as the content.. Commented Aug 31, 2013 at 2:26
  • Fiddler 2 is GREAT at sniffing network access and seeing exactly what is going on. Commented Aug 31, 2013 at 4:46

1 Answer 1

1

Instead of echo NULL try returning an object that has a single property having value null

e.g.

obj
{
returnValue = NULL
}

I am not sure of PHP syntax but what i want to say is to wrap your return value in an object. and on client side you will get obj.returnValue as NULL instead of empty string

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That worked fine. I had to JSON serialize the object to echo it and then parse it in jQuery. That's the whole problem as I didn't know that you can only echo strings.

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.