简体   繁体   English

使用AJAX和jQuery将JSON数据从一页发布到另一页

[英]Posting JSON data from one page to another using AJAX and jQuery

I have 我有

1.) textarea 1.)textarea

2.) select box 2.)选择框

3.) Button 3.)按钮

I pass JSON input in textarea, and onclicking the button json data is passed to another php page "sendingInterface.php" from where json data is passed through function to one more page called "index.php" where decoding of json and inserting into database takes place. 我在文本区域中传递JSON输入,然后单击按钮json数据被传递到另一个php页面“ sendingInterface.php”,JSON数据从该页面通过函数传递到另一个名为“ index.php”的页面,在该页面中,json解码并插入数据库发生。 The response of this is coming in the another textarea. 对此的响应将在另一个文本区域中出现。

My html code: 我的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 code:sendingInterface.php 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);
    }
    ?>

My commented PHP code is working properly. 我评论的PHP代码正常运行。 But I want to use file_get_contents("php://input") to get the json code. 但是我想使用file_get_contents(“ php:// input”)来获取json代码。

file_get_contents("php://input"); will read raw data from request body only for POST requests. 将仅从POST请求的请求主体读取原始数据。 If you have planned to changed your requests from GET to POST then you can use the following code in jquery to pass data to be available on php://input stream. 如果您计划将请求从GET更改为POST,则可以在jquery中使用以下代码来传递数据,以供php://input流使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM