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.

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

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。