简体   繁体   English

GET请求在Django框架中返回[object Object]

[英]GET request returns [ object Object] in django framework

I have successfully created an API using django framework, and http://127.0.0.1:8000/tuto/users/ in the browser get me the user list, Now i want to dislay it with on my web page, yet it returns [ object Object] 我已经成功地使用django框架创建了一个API,并且在浏览器中http://127.0.0.1:8000/tuto/users/在浏览器中获得了用户列表,现在我想将其放在我的网页上,但它返回[对象]

    <html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  </head>
  <body>
    <div id="users">
      <p>No users are available.</p>
    </div>
    <script>
    $(document).ready(function() {
      $.get("/tuto/users/").success(function (data) {
        window.alert(data)
        }
      });
    });
    </script>
  </body>
</html>

I get the alert message of [object Object] 我收到[object Object]的警报消息

This is probably happening because you are displaying the entire object, instead of just some of its properties. 这可能是因为您正在显示整个对象,而不是仅显示其某些属性。 Try changing window.alert(data) to 尝试将window.alert(data)更改为

window.alert(data.name) window.alert(data.name)

Change name property to something you have in your user class. 将name属性更改为用户类中的内容。

If you want to see the JSON data returned by your API in textual form, you can also do: 如果要查看API以文本形式返回的JSON数据,您还可以执行以下操作:

JSON.stringify(data)

Better ways to view your API data during development 在开发过程中查看API数据的更好方法

While The above can help during development, there are better ways than window.alert() , eg if you use console.log(data) you'll get a navigable tree view of your object in your browser console. 尽管上述内容可以在开发过程中提供帮助,但是有比window.alert()更好的方法,例如,如果您使用console.log(data) ,则将在浏览器控制台中获得对象的可导航树视图。

The network tab of your browser can also provide an intelligible view of your data when it's received from your Django API. 当从Django API接收到数据时,浏览器的网络选项卡还可以提供数据的清晰视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM