doulei2100 2017-03-13 07:23
浏览 94

无法使用JSON从Server获取响应

I am trying to get response from my server which is running php file login.php. I am running t3.html on computer and login.php on my server

t3.html

     <!DOCTYPE html>
       <html>
       <head>
       <meta charset="UTF-8">
       <title>PHP JSON Testing</title>
    <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
   </head>
   <body>
    <div id="form">
        <table>
            <tr>
                <td>Field 1</td>
                <td><input type="text" id="text1" /></td>
            </tr>
            <tr>
                <td>Field 2</td>
                <td><input type="text" id="text2" /></td>
            </tr>
            <tr>
                <td colspan="2"><button id="submit">Send Request</button>          </td>
            </tr>
        </table>
    </div>
    <div>
        Response:
        <span id="response"></span>
    </div>
    <script type="text/javascript">

        $(document).ready(function(){
            $("#submit").on("click", function(event){
                var text1 = $("#text1").val();
                var text2 = $("#text2").val();
                var jsonData = {
                    field1 : text1,
                    field2 : text2
                };
                $.ajax({
                    type: "POST",
                    url: "http://X.X.X.X/test/login.php",
                    data: JSON.stringify(jsonData),
                    contentType: "application/json",
                    dataType: "json",
                    success: function(response) {
                        $("#response").html(JSON.stringify(response));
                    },
                    failure: function(error){
                        $("#response").html(error);
                    }
                });
                $("#text1").val("");
                $("#text2").val("");
            });
        });
    </script>
</body>
</html>

login.php

<?php 
$json = file_get_contents("php://input");
$obj = json_decode($json);
$response = array();
$response["data1"] = $obj->field1;
$response["data2"] = $obj->field2;
$json_response = json_encode($response);
echo $json_response;``
header("Content-type: application/json");
?>

But it is not getting any response, but when i kept my html file on the server it worked please help me so that i can run the html file on my computer and get a response from server login.php

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计