将Localhost Asp.net MVC控制器json连接到前端

时间:2019-04-24 14:12:19

标签: javascript c# html asp.net-mvc

编辑: 我将html文件放到项目中,似乎现在没有localhost部分了,但是当我按下按钮时,它向我显示了4行,这是唯一的问题,我不明白为什么它会给我这个错误(我开始立即搜索)

未定义:$ undefined 未定义:$ undefined 未定义:$ undefined 未定义:$ undefined

我需要从localhost Asp.net MVC后端检索表的列表,但是我不明白我的代码有什么问题。

我没有任何搜索结果。不幸的是,我在网上找到了一些可以正常工作的代码,但不幸的是,我的后端却没有。

我被困了4天,需要帮助。

前部结束

<!DOCTYPE html>
<html>
<body>

<ul id = "myList" >

</ ul >

< p > Click the button to append an item to the end of the list.</p>

<button onclick = "myFunction()" > Click Me</button>

<script>
function myFunction() {

    var request = new XMLHttpRequest()


request.open('GET', 'http://localhost:59946/Transactions/Gettami', true)
   request.onload = function() {
        // Begin accessing JSON data here
        var data = JSON.parse(this.response)

  if (request.status >= 200 && request.status < 400)
        {
            data.forEach(movie => {

                var node = document.createElement("LI");
                var textnode = document.createTextNode("ElemntFind");
                node.appendChild(textnode);
                document.getElementById("myList").appendChild(node);

            })
  }
        else
        {
            console.log('error')
      }
    }

    request.send()
   }
</script>
</body>
</html>

后退END

 [HttpGet]
 public ActionResult Gettami()
 {

     var orderForBooks = from bk in _db.transactions
                         join ordr in _db.products
                         on bk.product_id equals ordr.Id
                         into a
                         from b in a.DefaultIfEmpty()
                         select new
                         {
                             bk.Id,
                             bk.user_id,
                             bk.product_id,
                             product_name = b.description,
                             bk.amount,
                             bk.currency
                         };

     List<TransactionView> Lista = new List<TransactionView>();

     foreach (var item in orderForBooks)
     {
         TransactionView nuovo = new TransactionView();
         nuovo.Id = item.Id;
         nuovo.user_id = item.Id;
         nuovo.product_id = item.product_id;
         nuovo.product_name = item.product_name;
         nuovo.amount = item.Id;
         nuovo.currency = item.Id;

         Lista.Add(nuovo);
     }



     return Json(Lista, JsonRequestBehavior.AllowGet);
 }

我希望得到类似以下结果:(按按钮尝试)

 <!DOCTYPE html>
 <html>
 <body>

 <ul id = "myList" >

 </ ul >

 < p > Click the button to append an item to the end of the list.</p>

 <button onclick = "myFunction()" > Click Me</button>

 <script>
 function myFunction() {

     var request = new XMLHttpRequest()

 request.open('GET', 'https://ghibliapi.herokuapp.com/films', true)
    request.onload = function() {
         // Begin accessing JSON data here
         var data = JSON.parse(this.response)

   if (request.status >= 200 && request.status < 400)
         {
             data.forEach(movie => {

                 var node = document.createElement("LI");
                 var textnode = document.createTextNode("ElemntFind");
                 node.appendChild(textnode);
                 document.getElementById("myList").appendChild(node);

             })
   }
         else
         {
             console.log('error')
       }
     }

     request.send()
    }
 </script>
 </body>
 </html>

0 个答案:

没有答案