AJAX works on localhost, but doesn't on live server

前端 未结 6 1339
滥情空心
滥情空心 2021-01-28 08:32

Below code works on localhost, but not on live server.

MAIN EDIT:

Only 1 thing remains which is not working:

<
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-28 09:24

    I think a few of the other posters are on to something about the invalid JSON,

    I would add however, this is something I like to do for JSON

    What this does is turn on output buffering. Which traps any output and buffers it. This includes warnings, notices, echo, and print stuff. Then it stuffs it into the response as debug and forwards it to the client.

    Obviously you would not want to do this on live production server, but you can easily comment it out. It can be a security issue to include some errors and stack trace information to the client. But for debugging purposes it works great.

    The problem with JSON is if you are checking the value of something somewhere (printing it) or have any notices it will muck up your JSON. For example

     printed content
     {"foo":"bar"}
    

    So this takes away that problem entirely (assuming you output buffer before printing anything) like so:

     {"foo":"bar", "debug":"printed content"}
    

    And now you have valid JSON, and as a side bonus you can print out your debug info by simply doing

     $.ajax({
         url: "controller.php",
         type: "POST",
         data: post,
         cache: false,
         dataType: "json",
         beforeSend: function () {
           saveScore();
         },
         success: function (data) {
              if(data.debug) console.log(data.debug);
         }
     });
    

    It's simple and effective.

    Hope it helps.

提交回复
热议问题