0

I am facing a problem when returning json from a domain.

In one file i have the following

var postData ="domain=testing.gr";

$.ajax({
    type: "POST",
    dataType: "json",
    data: postData,
    beforeSend: function(x) {
        if(x && x.overrideMimeType) {
            x.overrideMimeType("application/json;charset=UTF-8");
        }
    },
    url: 'http://www.ewebs.gr/advprodigy/adv.php',
    success: function(data) {
        // 'data' is a JSON object which we can access directly.
        // Evaluate the data.success member and do something appropriate...
        if (data.success == true){
            $('#keimeno').html(data.message);
        }
        else{
            $('#keimeno').html(data.message);
        }
    }
});

...and in the PHP file I have hardcoded the following:

<?php header('content-type: application/json');

    $domain = $_POST['domain'];

    // Set up associative array
    $data = array('success'=> true,'message'=>'Success message: hooray!');

    // JSON encode and send back to the server
    echo json_encode($data);

?>

Yet i get an error on returning.. response is empty! http://prntscr.com/x333k

James Donnelly
  • 126,410
  • 34
  • 208
  • 218

3 Answers3

0

Here you did not got json from your server so try this,

<?php 
    //header('content-type: application/json');

    //$domain = $_POST['domain'];

    // Set up associative array
    $data = array('success'=> true,'message'=>'Success message: hooray!');

    // JSON encode and send back to the server
    echo json_encode($data);
?>

If this works then add this error_reporting(-1); to your php file to get error.

Also read this for header json https://stackoverflow.com/questions/267546/correct-http-header-for-json-file

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

Try to have this on the server:

header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json'); 
Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
-1

Try to use

{
type: "POST",
contentType : "application/json; charset=utf-8",
dataType : "json",...}
Damian0o
  • 653
  • 5
  • 15