weixin_33724059 2016-03-20 01:38 采纳率: 0%
浏览 14

简单的PHP和AJAX问题

I appear to be having some sort of odd jQuery AJAX issue. I'm trying to get PHP code to execute using the isset() function and jquery on page load. Here's my jQuery code (it's already nested inside a document.ready function, and the code immediately before it runs perfectly):

$.ajax({
  url: url,
  data: {test: "test"},
  post: "POST"
}).done(function(data) {
  if (!data.error) {
    alert("Test");
  }
});

The callback function on completion of the ajax request is running. And then here's my PHP code:

if (isset($_POST["test"])) {

  echo "Test";

}

I don't understand why it's not echoing. It's probably something ridiculously simple that I'm overlooking, but I've spent about 45 minutes on this. Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • 旧行李 2016-03-20 01:49
    关注

    Change post: "POST" to type: "POST"

    Without that, it will fail silently and fire off a GET request.

    Also, keep in mind that the network tab in your browser's dev tools can be your best friend when dealing with ajax. It will tell you every request the browser makes, what method it uses, parameters and response, and all that.

    Browser dev console...learn it, love it. You won't know how you lived without it.

    评论

报告相同问题?