Javascript ASP.NET Core/jQuery-尝试显示JSON时出现未捕获类型错误

Javascript ASP.NET Core/jQuery-尝试显示JSON时出现未捕获类型错误,javascript,c#,jquery,asp.net-core,Javascript,C#,Jquery,Asp.net Core,我有一个TCP侦听器,它正在侦听包含酒店id和名称的传入JSON字符串,一旦收到JSON,我想显示一个酒店列表,并将其名称作为列表显示。但是,在chrome控制台中,我遇到以下错误: jquery.js:489 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in "[{\"Id\":1,\"Name\":\"Hotel Ritz\"},{\"Id\":2,\"Name\":\"Scandic\"},{\"I

我有一个TCP侦听器,它正在侦听包含酒店id和名称的传入JSON字符串,一旦收到JSON,我想显示一个酒店列表,并将其名称作为列表显示。但是,在chrome控制台中,我遇到以下错误:

jquery.js:489 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in "[{\"Id\":1,\"Name\":\"Hotel Ritz\"},{\"Id\":2,\"Name\":\"Scandic\"},{\"Id\":3,\"Name\":\"Hotel Villa Provence\"},{\"Id\":4,\"Name\":\"First Hotel Atlantic\"}]"
at isArrayLike (jquery.js:489)
at Function.each (jquery.js:351)
at Object.success (site.js?v=5BUCGd0L7FqRV9EDqjbCXfE3SLPjEtoSfd2jy5c48yU:26)
at fire (jquery.js:3268)
at Object.fireWith [as resolveWith] (jquery.js:3398)
at done (jquery.js:9305)
at XMLHttpRequest.<anonymous> (jquery.js:9548)
ModelManager.cs:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;sing System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace VIABooking.Models
{
public class ModelManager : IModelManager
{
    private readonly TcpConnectionManager tcpManager = new TcpConnectionManager();

    public ModelManager() {}

    public string GetHotelList ()
    {
        return tcpManager.GetHotelList();
    }
}
}
TCPListener.cs:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Formatters.Json;

namespace VIABooking.Models
{
public class TcpConnectionManager
{        
    public string GetHotelList()
    {
        //Sending json to other server

        const int PORT_NO = 5005;
        const string SERVER_IP = "127.0.0.1";

        TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
        NetworkStream ns = client.GetStream();

        string s = "Please send me a list of all hotels!";

        string json = JsonConvert.SerializeObject(s);

        byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(json);

        ns.Write(bytesToSend, 0, bytesToSend.Length);


        //Receiving json from other server

        byte[] buffer = new byte[client.ReceiveBufferSize];

        int bytesRead = ns.Read(buffer, 0, client.ReceiveBufferSize);

        string dataReceived = Encoding.UTF8.GetString(buffer, 0, bytesRead);

        string json2 = JsonConvert.SerializeObject(dataReceived);

        client.Close();
        ns.Close();

        return json2;
    }
}
}

您可以使用下一个选项之一来解决您的问题:

  • consthotels=JSON.parse(data)
    在客户端获取js对象 而不是字符串
  • dataType:json
    添加到
    $的设置参数中。ajax
  • 返回
    List
    JsonResult
    而不是服务器端的
    api/hotels
    中的字符串。它会将响应的内容类型更改为application/json,所以jquery将自动解析json
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using VIABooking.Models;

namespace VIABooking.Controllers
{
public class HomeController : Controller
{

    private readonly ModelManager modelManager = new ModelManager();

    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Hotel()
    {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}
}
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;sing System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace VIABooking.Models
{
public class ModelManager : IModelManager
{
    private readonly TcpConnectionManager tcpManager = new TcpConnectionManager();

    public ModelManager() {}

    public string GetHotelList ()
    {
        return tcpManager.GetHotelList();
    }
}
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Formatters.Json;

namespace VIABooking.Models
{
public class TcpConnectionManager
{        
    public string GetHotelList()
    {
        //Sending json to other server

        const int PORT_NO = 5005;
        const string SERVER_IP = "127.0.0.1";

        TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
        NetworkStream ns = client.GetStream();

        string s = "Please send me a list of all hotels!";

        string json = JsonConvert.SerializeObject(s);

        byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(json);

        ns.Write(bytesToSend, 0, bytesToSend.Length);


        //Receiving json from other server

        byte[] buffer = new byte[client.ReceiveBufferSize];

        int bytesRead = ns.Read(buffer, 0, client.ReceiveBufferSize);

        string dataReceived = Encoding.UTF8.GetString(buffer, 0, bytesRead);

        string json2 = JsonConvert.SerializeObject(dataReceived);

        client.Close();
        ns.Close();

        return json2;
    }
}
}