dongyi1524 2017-02-01 23:31
浏览 72
已采纳

如何使用jQuery.post将JSON数据发送到php脚本?

This is in my javascript code. The variable dataStore is a valid JSON object and the JSON.stringify works fine - I tested with console.log() to be sure.

I'm new to jQuery and have not written a CGI application in awhile so I may be doing something incorrect here.

jQuery.post("taskmanager.php", JSON.stringify(dataStore), function (returnData) { 
    alert(returnData); 
});

The taskmanager.php script resides in the same folder as my other files ( taskmanager.html, taskmanager.css, taskmanager.js ). This folder resides on my Windows7 folder E:\JamesLaderoute\MySoftware\WebApp\TaskManager\code

I'm not sure what to put into the taskmanager.php script. I thought I could do something simple like:

<?php

    $data = $_GET["data"];

    echo "done";

?>

What I don't understand is, how will the php script know that my JSON stringify information was called "data".

Also, I thought jQuery.post() takes the script name as the first argument, and then the data to be sent to the script followed by a callback function that gets called when the script returns data via echo calls.

The alert() does get called but it's displaying the php script rather than the string "done"

I've searched the internet (a little bit) and found examples that use something called AJAX to send data to a script like php.

I learn best with a small example, is there a good example that points out what I'm missing? How do I get this to work?

Eventually I plan on taking whatever the JSON data is and saving it to a file on the server. Do I need to run this all with a real server? Right now I'm not using one but I do have WAMP setup so I could use that if that is what is required.

Thank you to everyone who posted answers to my problem. I'm editing this post to show the actual code I ended up using to get my application working.

JavaScript code:

alert(returnData); });
        $.ajax({
            url : "taskmanager.php" ,
            type : 'POST',
            data : JSON.stringify(dataStore),
            success : function(res) {
                    // Successfully sent data
                  console.log(res);
            },
            error: function(err) {
                // Unable to send data
                  console.log(err);
            }
        });

PHP code:

<?php

    $contents = file_get_contents('php://input');
    $data = json_decode( $contents );

    file_put_contents( "taskdata.json",  "data=".$contents );

    echo "<br>done</br>";

?>
  • 写回答

3条回答 默认 最新

  • doujiyan0031 2017-02-01 23:36
    关注

    Using jQuery you can send data to your end point using the POST protocol. Example below

    $.ajax({
        url : 'taskmanager.php',
        type : 'POST',
        data : JSON.stringify(dataStore),
        success : function(res) {
            // Successfully sent data.
            console.log(res);
        },
        error: function(err) {
            // Unable to send data.
            console.log(err);
        })
    });
    

    You may also use the $.post method, its just a preference. $.ajax provides with some extra flexibility, in case you maybe want to update your protocol to GET instead of post or numerous other instances...

    More info on this here: http://forum.jquery.com/topic/what-should-i-use-post-vs-ajax

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 主要进行描述非满管状态下,管路的摩阻系数是怎么变化的,在管路长度方向上是怎么分布的(标签-matlab)
  • ¥15 multisim设计求解如图
  • ¥15 动态多目标优化,求解定义的问题的POF和POS公式
  • ¥30 实验 :UML2.0 行为建模
  • ¥15 设计一种优化调度算法并结合论文中的案例给出调度计划
  • ¥15 py文件打包exe报错TypeError:expected str,bytes or os.PathLike object,not_io.BytesIO
  • ¥15 Java编写学籍信息管理系统
  • ¥15 IPXE网刻客户机找不到服务端
  • ¥15 小贝360-4 配二个 华772S 设置WⅰFi5G 连接
  • ¥15 vs2022的QT报错,好像是缺少winextras