如何使用jQuery.post将JSON数据发送到php脚本? [英] How do I use jQuery.post to send JSON data to a php script?

查看:64
本文介绍了如何使用jQuery.post将JSON数据发送到php脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在我的JavaScript代码中.变量dataStore是有效的JSON对象,并且JSON.stringify可以正常工作-我确定使用console.log()进行了测试.

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.

我是jQuery的新手,已经有一段时间没有编写CGI应用程序了,所以我在这里可能做错了什么.

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); 
});

taskmanager.php脚本与其他文件(taskmanager.html,taskmanager.css,taskmanager.js)位于同一文件夹中.该文件夹位于我的Windows7文件夹 E:\ JamesLaderoute \ MySoftware \ WebApp \ TaskManager \ code

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

我不确定要在taskmanager.php脚本中添加什么.我以为我可以做一些简单的事情,例如:

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";

?>

我不明白的是,php脚本如何知道我的JSON字符串化信息称为数据".

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

此外,我认为jQuery.post()会将脚本名称作为第一个参数,然后将要发送到脚本的数据发送给脚本,随后是一个回调函数,当脚本通过 echo <返回数据时将调用该回调函数/strong>电话.

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.

alert()确实被调用,但是它显示的是php脚本而不是字符串"done"

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

我已经搜索了一下互联网,并找到了一些示例,这些示例使用称为AJAX的东西将数据发送到php之类的脚本.

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?

最终,我计划采用任何JSON数据并将其保存到服务器上的文件中.我是否需要在真实服务器上运行所有这些?现在,我没有使用WAMP,但是我有WAMP设置,因此如果需要的话,可以使用它.

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.

感谢所有发布了我的问题答案的人.我正在编辑这篇文章,以显示最终用来使应用程序正常工作的实际代码.

JavaScript代码:

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代码:

<?php

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

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

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

?>

推荐答案

使用jQuery,您可以使用POST协议将数据发送到端点.下面的例子

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);
    })
});

您还可以使用$ .post方法,它只是一个首选项.$ .ajax提供了一些额外的灵活性,以防您可能想将协议更新为GET而不是发布或其他许多实例...

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...

此处的更多信息: http://forum.jquery.com/topic/what-should-i-use-post-vs-ajax

这篇关于如何使用jQuery.post将JSON数据发送到php脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆