douma5954 2017-01-18 23:01
浏览 200


If this is a re-post of an already existing. I looked a bunch of different issue already posted about this but didn't feel any matched my issue. If there is one please link it.

I'm trying to learn AJAX and have a form that takes two inputs a serverName, and a isLive. As well as a button to add a server to the list. I already made a json file that has two servers in it and when I GET info from it to update the list it lists the server's serverName and isLive fine. When I go to POST information all I get back when the list is updated is undefined.

Using console logs I know that the information is being passed from the form to Object to be sent by AJAX and that it seems to trigger the success function, but when I look there is nothing added to the Json and it says the fields are undefined.


<!DOCTYPE html>
        <h1>Server List</h1>
        <ul id="servers">
        <h4>Add a Server</h4>
        <p>Server Name: <input type="text" id="serverName"></p>
        <p>Offline or Online: <input type="text" id="isLive"></p>
        <button id="add-server">Add!</button>

        <script src=""></script>
        <script src="main.js"></script>


$(function() {
    var $servers = $('#servers');
    var $serverName = $('#serverName');
    var $isLive = $('#isLive');
        type : 'GET',
        url: 'servers.json',
        success: function(servers) {
            $.each(servers, function(i, server) {
                $servers.append('<li>Server: '+ server.serverName +', Status: '+server.isLive+'<li>');
        error: function() {
            alert('Error loading server status!');

    $('#add-server').on('click', function() {
        var server = {
            serverName: $serverName.val(),
            isLive: $isLive.val(),
            type: 'POST',
            url: 'servers.json',
            data: server,
            success: function(newServer) {
                $servers.append('<li>Server: '+ newServer.serverName +', Status: '+newServer.isLive+'<li>');
            error: function(){
                alert('error saving server');


[{"serverName":"vanilla","isLive":"offline"}, {"serverName":"SkyFactory","isLive":"Online"}]
  • 写回答

1条回答 默认 最新

  • douliao5942 2017-01-18 23:36

    You can't modify a file in the server with JavaScript. Check this may help: Use JQuery to Modify external JS File

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 求局部放电案例库,用于预测局部放电类型
    • ¥100 QT Open62541
    • ¥15 stata合并季度数据和日度数据
    • ¥15 谁能提供rabbitmq,erlang,socat压缩包,记住版本要对应
    • ¥15 Vue3 中使用 `vue-router` 只能跳转到主页面?
    • ¥15 用QT,进行QGIS二次开发,如何在添加栅格图层时,将黑白的矢量图渲染成彩色
    • ¥50 监控摄像头 乐橙和家亲版 保存sd卡的文件怎么打开?视频怎么播放?
    • ¥15 Python的Py-QT扩展库开发GUI
    • ¥60 提问一下周期性信信号的问题
    • ¥15 jtag连接不上fpga怎么办