dongpao9437 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){
    console.log(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?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?