??yy 2017-02-22 00:26 采纳率: 0%
浏览 74

Mongodb的Ajax / jQuery

I'm quite new at Ajax/jQuery. I am trying to create a sample HTML page based on Ajax/jQuery for getting all the customers and searching a customer by ID. Each customer has three variables: ID, firstName and lastName.

I want to create, delete and update a customer. How can this be implemented?

Current HTML with javascript:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
    <input name="search" type="text" maxlength="100" id="search"/>
    <button onclick="searchID()"> Search ID </button>
     <button onclick="showAll()"> Show All </button>
      <button onclick="createCustomer"> CreateCustomer </button>
      <button onclick="updateCustomer"> UpdateCustomer </button>
      <button onclick="deleteCustomer"> DeleteCustomer </button>


    <div id="persons"></div>

    <script>
        function searchID()
        {
            var id = document.getElementById("search").value;
            $("#persons").html("");


            $.getJSON("http://192.168.17.128:8080/customers/" + id,  function(data)
            {
                for (var i in data) {
                    $('#persons').append("<p>ID: " + data[i].id + "</p>")
                    $('#persons').append("<p>First name: " + data[i].firstName + "</p>")
                    $('#persons').append("<p>Last name: " + data[i].lastName + "</p><br>")
                }
            });


        }

        function showAll()
        {
            $("#persons").html("");

            $.getJSON("http://192.168.17.128:8080/customers/",  function(data)
            {
                for (var i in data) {
                    $('#persons').append("<p>ID: " + data[i].id + "</p>")
                    $('#persons').append("<p>First name: " + data[i].firstName + "</p>")
                    $('#persons').append("<p>Last name: " + data[i].lastName + "</p><br>")
                }
            });
        }
    </script>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • weixin_33749242 2017-02-22 03:23
    关注

    You have tackled the display part. That is good. With regard to adding/updating/deleting, there are many different methods and formats that you can use.

    If you do a search on the internet, you will find words like REST, SOAP, and more. Each of these is a method of communication with a server that defines how the server will receive requests. Which method you choose is not important. And, you can even use no method at all, if you like, and make something entirely your own.

    Here is what is important:

    1. Decide what signal you will send to the server.

      For an update request, you will want to include all the person data plus the record id for the person who is being updated.

      For a create request, you should not send anything, but you should receive the id of a newly created person. OR, you could send over a complete person without an ID and have the server respond with the ID that has been assigned... Its YOUR choice!!!

      For a delete request, start with a simple system and send only the ID of the person who is being removed. Later, you can add other parameters to check if the user is sure about the deletion request. Start simple, and build from there.

    2. Decide what signal will be received after the request is sent

      Some of this we talked about in step one. For example, when we create a new person, we will want to see the new id for that person in the response from the server. If we are not sending the new person ID, we will at least want a confirmation code that indicates that the person has been created.

      Think about what you will want to see from the browser-side to confirm a successful update and delete. What do you want errors to look like from the browser-side?

      Once you have all of this, you can start creating the code.

    3. Create the server

      Once you decide on a method, you can start creating a server. For the purpose of discussion, let us use the following. We will send update requests, to mysite.com/person/update.php. We will send delete requests to mysite.com/person/delete.php, and create requests to mysite.com/person/create.php. You will notice that I have selected PHP as my server side language for this example.

      We will use POST to carry all the data that we are sending, and we will receive data at the browser in JSON format.

      Once we have this plan, we can create three PHP scripts to do the work that we need. The update script might look like this:

    person/update.php:

     $id = $_POST['id'];
     $fname = $_POST['first_name']; 
     $lname = $_POST['last_name'];
    
     /* perform some checking for SQL injection attacks */
    
     /* use the ID to update the database */
    
     /* prepare a response to the web browser */
     $response = new StdClass();
     $response->type = "update";
     $response->id   = $id;
     $response->status = "SUCCESS";
     echo( json_encode( $response ) );
     /* end file after outputting JSON response */
    
    1. Add AJAX calls to your client (the web site)

      In the example above, we define a few key things. We are looking for values in the post fields named 'id', 'first_name', and 'last_name', so our web client needs to send the form data in these three fields.

      Also, in the example above, we set three values in the response: 'type', 'id', 'status'. Our AJAX code should know how to look at these values and decide whether further action is required, or display a success result to the site's user, because we all want some sort of confirmation that what we are doing is actually working.

      Keep in mind that these values are values that I picked on the spot, which means that the developer (That's you), has full control.

      Design your AJAX to send and receive based on the decisions you make in steps 1, 2, and 3.... if you have trouble along the way, come back to Stack Overflow. Tell us about your idea and what you are trying to do and the work you have done. Share with us your code, and ask for further advice.

    As it stands, your question is far too broad for us to be able to answer you with more specific examples, because all of the decisions are in your hands. I hope that my answer has given you some things to think about and will help you get moving toward the next step.

    Happy Coding!

    P.S. It is possible to send queries directly to a database server. I recommend against this. Keep your web applications light (small in size) and let server applications handle the heavy work like database communications.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器