繁体   English   中英

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

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

我有

1.)textarea

2.)选择框

3.)按钮

我在文本区域中传递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代码。

file_get_contents("php://input"); 将仅从POST请求的请求主体读取原始数据。 如果您计划将请求从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