【问题标题】:Posting JSON data from one page to another using AJAX and jQuery使用 AJAX 和 jQuery 将 JSON 数据从一个页面发布到另一个页面
【发布时间】:2023-03-24 15:12:01
【问题描述】:

我有

1.) 文本区域

2.) 选择框

3.) 按钮

我在 textarea 中传递 JSON 输入,单击按钮 json 数据被传递到另一个 php 页面“sendingInterface.php”,json 数据从该页面通过函数传递到另一个名为“index.php”的页面,其中解码 json 和插入数据库发生。对此的响应来自另一个文本区域。

我的html代码:

<form id="data" action="" method="post" >

            <h3>Request</h3>
            <textarea name="jsondata" id="jsondata" value="">   </textarea>
            <br>
            <select name="listoptions" id="listoptions">
                <option selected="selected" value="">Select method</option>
                <option value="sendIncidentReport">sendIncidentReport</option>
                <option value="sendDeathReport">sendDeathReport</option>
</select>
            <br>
             <input type="button" name="send_report" id="send_report" value="Send Report">
             <br>
<h3>Response</h3>
         <br>
         <textarea name="response" id="response" rows="8" cols="8" value="">   </textarea>

<script>
    var url="http://localhost/API/sendingInterface.php?fn="
$(document).ready(function() {
        $("#send_report").click(function () {

        $.ajax({
        url:url + "sendIncidentReport", // calling url
        type: 'GET',
        data:{jsondata:$("#jsondata").val()         
        },
        cache: false,
        async: false,
        success: function(data) {
           $('#response').val(data);
        },
        error: function() {
        alert('Error');
        }
        });
});
}); 

$(document).ready(function() {
        $("#send_report").click(function () {

        $.ajax({
        url:url + "sendDeathReport", // calling url
        type: 'GET',
        data:{jsondata:$("#jsondata").val()         
        },
        cache: false,
        async: false,
        success: function(data) {
           $('#response').val(data);
        },
        error: function() {
        alert('Error');
        }
        });
});
});
</script>

PHP代码:sendingInterface.php

    <?php
    include_once 'index.php';
    $obj=new send();

    /*sendIncidentReport*/
    if($_GET['fn'] =="sendIncidentReport")
    {  
         $json=file_get_contents("php://input");
         $obj->sendIncidentReport($json);
        //$jsondata=$_GET['jsondata'];
        //$obj->sendIncidentReport($jsondata);
    }
    /*sendDeathReport*/
    if($_GET['fn'] =="sendDeathReport")
    {
          $json=file_get_contents("php://input");
          $obj->sendDeathReport($json);
        //$jsondata=$_GET['jsondata'];
        //$obj->sendDeathReport($jsondata);
    }
    ?>

我注释的 PHP 代码运行正常。但我想使用 file_get_contents("php://input") 来获取 json 代码。

【问题讨论】:

    标签: php jquery json


    【解决方案1】:

    file_get_contents("php://input"); 将只为 POST 请求从请求正文中读取原始数据。 如果您计划将请求从 GET 更改为 POST,那么您可以在 jquery 中使用以下代码来传递数据,以便在 php://input 流上可用。

    $.ajax('url',{
    'data': JSON.stringify(yourJSONObject), //{jsondata:$("#jsondata").val()}
    'type': 'POST',
    'processData': false,
    });
    

    【讨论】:

    • 您的代码运行良好。但我需要我的 json 数据来自 textarea。所以我不能使用 JSON.stringify(yourJSONObject) 吗?
    • 像这样传递一个带有你的 textarea 值的对象:JSON.stringify( {jsondata: $("#jsondata").val()} )
    猜你喜欢
    • 1970-01-01
    • 2016-04-05
    • 2014-05-15
    • 2013-01-24
    • 2012-12-21
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    相关资源
    最近更新 更多