1

I am new to javascript and query. This is my first attempt on it.

My code is pretty simple as follows:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#get").click(function() {
            $.ajax( {
                url: "get_port.php",
                type: "GET",
                success: function(result) {
                console.log(result);
                },
                error: function(data) {
                console.log(data)
                }
            });
        });
    });
</script>

<style>
    .button {
        background-color: #4CAF50;
        color: #fff;
        cursor: pointer;
        border: none;
        color: white;
        padding: 15px 25px;
        border-radius: 20px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 24px;
        box-shadow: 0 5px #999;
    }

    .button:hover {
        background-color: #3E8E41;
    }

    .button:active {
        background-color: #3E8E41;
        box-shadow: 0 1px #666;
        transform: translateY(4px);
    }
</style>
</head>

<body>  
    <div align="center">
        <form>
            <p style="font-size: 30px;">Current Port:&nbsp
                <span id="port_name"
                    style="font-size: 30px;">Unknown
                </span>
            </p>
            <button class="button" id="get">Get</button>
        </form>
    </div>
</body>
</html>             

My php code:

<?php
    $ss_json = file_get_contents("/etc/shadowsocks.json");
    $ss_config = json_decode($ss_json, true);
    foreach($ss_config['port_password'] as $key => $value) {
        echo $key;
    }
?>

And I get no response from my php file with status code 0. My php is just a simple echo sentence. The return type is also not json. Someone please figure this out for me!!

I have been working on it for an entire day and I have literally no clue for solving this problem.

My error message:

{readyState: 0, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function, …}

Also I have checked the access log and it shows the following log:

"GET /get_port.php HTTP/1.1" 200 199"
Alex Zhao
  • 11
  • 2
  • have you tried adding `contentType: 'application/json'` ? – Aziza Kasenova Aug 31 '21 at 23:02
  • Yes. I have tried almost every solution I could find and they do not work – Alex Zhao Aug 31 '21 at 23:10
  • Hi Alex, post as much of the related code as is needed. So, the php thats being called up, the javascript that is callling it and any involved forms. When we see only one side of the equation we can only guess as to the issue. You will get better answers with better code so see. +1 from me for using code blocks here on SO, its a good start. – CodingInTheUK Aug 31 '21 at 23:18
  • look in devtools and see which address is being fetched, than try open that url directly in the browser – vanowm Aug 31 '21 at 23:20
  • Thank you for your suggestions. I have update my full code in the post. – Alex Zhao Aug 31 '21 at 23:58
  • Your form will be submitting normally. Add `type="button"` to your ` – Phil Sep 01 '21 at 23:35

0 Answers0