簡體   English   中英

如何通過 html 顯示從 JavaScript 檢索到的數據

[英]How to display retrieved data from JavaScript through html

我知道這看起來有點含糊,但這是我能用語言表達的最好方式。 下面是我的代碼示例。

JavaScript

    <script type="text/javascript">
        var name = $('#firstName').val();
        var email = $('#userEmail').val();
        var json = { Email: email, FirstName: name };
        var string = JSON.stringify(json);

        $.ajax({
            url: 'http://uBuildRewards.api/api/Users/GetInfo',
            type: 'GET',
            contentType: 'application/json',
            dataType: "json",
            crossDomain: true,
            data: string
        });
     </script>

HTML

<div class="user-logged-in">
   <div class="content">
       <div class="user-name" id="firstName"> **NAME HERE** <span class="text-muted f9">admin</span></div>
       <div class="user-email" id="userEmail"> **EMAIL HERE** </div> 
       <div class="user-actions">
           <a class="m-r-5" href="">settings</a> <a href="/first/login">logout</a>
       </div>
   </div>
</div>

在這之前,在另一個視圖中,我有類似的 JavaScript 代碼用於登錄。 這是從 UsersController 訪問獲取所有信息的方法,如果 email 和 pass 是正確的,它會讓我登錄。 as) 和您當前的電子郵件地址是 (我曾經以誰的身份登錄)。

所以我試圖找出如何從 javascript 中粘貼從“GetInfo”方法檢索到的信息,並在我放置“ NAME HEREEMAIL HERE ”的地方顯示 Name 和 Email

我試圖盡可能地進行描述。 很抱歉有任何混淆。 我非常感謝您提前提供的幫助 :) 對 JQuery 和 JavaScript 仍然相當陌生!

為什么要通過 http 而不是 https (SSL) 發送登錄憑據?

一邊...

你必須使用.done方法。

$.ajax({
    url: 'http://uBuildRewards.api/api/Users/GetInfo',
    type: 'GET',
    contentType: 'application/json',
    dataType: "json",
    crossDomain: true
}).done(function(data) {
      console.log("Sample of data:", data);
      $('#firstName').text(data.name);
      $('#userEmail').text(data.email);

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM