2016-08-09 18:50
浏览 88

$ .post显示在console.log()中,但无法访问我的php页面上的变量

I'm pretty inexperienced with jquery. My code is

function edit(uID){ 
  var php = "body/weekly_deals.php";
  var data = {"edit" : "post is here"}
  $.post(php, data,function(response){

This is being defined on a WeeklyDeal.php. Now, on my body/weekly_deals.php page, I var_dump($_POST['edit']) and I'm getting NULL, however in the console.log, I'm seeing the value "post is here" string? So, I'm confused. How can it be there, but not be there at the same time?

图片转代码服务由CSDN问答提供 功能建议

我对jquery缺乏经验。 我的代码是

  function edit(uID){
 var php =“body / weekly_deals.php”; 
 var data = {“edit”:“post is here  “} 
 $ .post(php,data,function(response){

这是在WeeklyDeal.php上定义的。 现在,在我的body / weekly_deals.php页面上,我var_dump($ _ POST ['edit'])并且我得到NULL,但是在console.log中,我看到值“post is here”字符串? 所以,我很困惑。 怎么会在那里,但不是在同一时间?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drj58429 2016-08-09 19:27

    I suspect you are misunderstanding how this works.

    I var_dump($_POST['edit']) and I'm getting NULL

    The way you phrased your question, it sounds like you are not seeing that null in your console.log(), which is what would happen if you had the var_dump and called it with ajax. Instead, it sounds like you are loading weekly_deals.php directly in the browser and, since that is a GET request, not a POST request, and no parameters are being passed, it comes back empty.

    however in the console.log, I'm seeing the value "post is here" string

    Right, because javascript is making an HTTP request using the POST method and passing a parameter.

    I think you may be confused about how an HTTP request works. To break it down, you have a resource which comes as a URI -- you know this as the web address. You can ask it questions in a few different ways -- GET, POST, PUT, etc. A web browser, when navigating to a page, issues a GET request to the resource. The web server returns the response and renders it.

    When you make an AJAX request, you are doing something very similar as far as the request life cycle is concerned. When you make the request, the server renders the response and sends it back. That is why your console.log() has what you expect to see -- because AJAX made the request the server side expected to get. When you navigate to the page directly in the browser, it is the wrong type of request and thusly you see the wrong response.

    解决 无用
    打赏 举报

相关推荐 更多相似问题