-3

Hi I am currently learning php and I am trying to get data from php file using the below script but i am not getting any response

        $.ajax({
            type: 'POST',
            url: "mark_mod.php",
            data: data_set,
            dataType: "JSON",
            success: function(data) { 
                alert("Response : " ); // not triggering
            }
        });

my php return stmt

7
  • 4
    Are youn echoing in your php file? Commented Sep 24, 2019 at 9:44
  • refer stackoverflow.com/a/57820130/6309457 OR stackoverflow.com/a/52404102/6309457 Commented Sep 24, 2019 at 9:46
  • 2
    Welcome. You should post the .php script that doesn't seem to work Commented Sep 24, 2019 at 9:48
  • 2
    please post your php code or check yourself are you echo or print the response Commented Sep 24, 2019 at 9:51
  • <?php $retJason = '[ { "userid":"'.$out_userid.'", "username":"'.$out_username.'", "address":"'.$out_address.'" } ]'; echo $retJason; ?> Commented Sep 24, 2019 at 10:20

4 Answers 4

0

There might be problems with File URL or file access. You can use complete callback to check request for errors like that:

 $.ajax({
    type: 'POST',
    url: "mark_mod.php",
    data: data_set,
    dataType: "JSON",

    success: function(data) { 
        alert("Response : " );
    },

    // This method will be fired when request completes
    complete: function(xxhr, status) {
        alert("Status code: " + status);
    }

});

If the status code is not success that means there is something wrong with your request. You can check more callback options here.

It doesn't matter whether you use return or echo in your PHP file. The success method must get a call if it's fine. However, if you use return instead of echo, nothing will append to your request's data.

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

Comments

0

On the other hand, The PHP response will include in your 'data' variable in the function success.

You need use data in the assync function success

success: function(data) { 
    alert("Response : " + data);
},

Comments

0

Thanks for your Responses. I got the Solution to my problem. It seems since Ajax is asynchronous... its taking time to receive the resultant echo value from my php file. so I had to synchronize my Jquery using async : False.

Comments

0
$(function(){
$('#formID').on('submit',function(){
const data_set={
name:"Nipu chakraborty",
"phone":"01783837xxx"
}       
$.ajax({
            type: 'POST',
            url: "mark_mod.php",
            data: data_set,
            dataType: "JSON",
            success: function(data) { 
                alert(data);
            }
        });
   });
});

Comments

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.