简体   繁体   中英

returning results with Json_encode

I am using an AJAX call with Jquery to a PHP form:

$.ajax({
    type : 'POST',
    url : 'submit_form.php',
    dataType : 'json',
    data:  dataString ,

which encodes the results from the database using:

echo json_encode($results);

However it prints the data back to at the top of the page that made the AJAX call, this completely breaks functionality in IE because it returns the text string before <html> declaration.

{"status":false,"msg":"Sorry The Queue is full"}<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

Is there anyway I can suppress this echo function on the page but still return the data array back to my page so I can use the results like ( data.msg )

Try to put it : header("Content-type: application/json"); before the code: echo json_encode($results); .

Best regards...

Use a callback function with your response data. The callback will be some function which handles the data received. It must be on the global scope (AFAIK). When the response from the server is ready, the function will be called and the data handled.

// from the server:
someFunction({"status":false,"msg":"Sorry The Queue is full"});

// in your page, in a script tag, etc.
window["someFunction"] = function(result){
    // do something
}
$.ajax({
  type : 'POST',
    url : 'submit_form.php',
    dataType : 'json',
  success: function(data) {
   console.log(data);
});

Try this.On successful calling you will get the result as js object. Check the result within console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM