Javascript 在ember js中使用response.json()从API获取响应时显示错误

Javascript 在ember js中使用response.json()从API获取响应时显示错误,javascript,java,api,ember.js,Javascript,Java,Api,Ember.js,我必须创建一个web应用程序,在mySQL数据库中添加一个用户,并使用ember js实现UI app/components/user.hbs <h1>User Management!</h1> <div> <label1>User Id </label1> <colon1>:</colon1> <input1>{{input type="text"

我必须创建一个web应用程序,在mySQL数据库中添加一个用户,并使用ember js实现UI

app/components/user.hbs

   <h1>User Management!</h1>
<div>   
    <label1>User Id </label1>   <colon1>:</colon1>
    <input1>{{input type="text" value=id placeholder="Enter id"}}</input1>
    <label2>Firstname</label2>  <colon2>:</colon2>
    <input2>{{input type="text" value=firstname placeholder="Enter firstname"}}</input2>
    <label3>Lastname</label3>   <colon3>:</colon3>
    <input3>{{input type="text" value=lastname placeholder="Enter lastname"}}</input3>
    <label4>Mail Id</label4>    <colon4>:</colon4>
    <input4>{{input type="text" value=mailid placeholder="Enter mailid"}}</input4>
</div>
    <button1 {{on "click" (fn this.user "add" id firstname lastname mailid )}}>Add User</button1>
    <button2 {{on "click" (fn this.user "delete" id firstname lastname mailid )}}>Delete User</button2>
Servlet API代码

//Required Header files
@WebServlet("/UserManagementServlet")
public class UserManagementServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException 
    {   res.setContentType("application/json");  
        res.setCharacterEncoding("UTF-8");
        Gson gson=new Gson();
        BufferedReader br=new BufferedReader(new InputStreamReader(req.getInputStream()));
        String param=br.readLine();
        User user = gson.fromJson(param, User.class);
        HashMap<String,String> jsonfile=new HashMap<String,String>();
        PrintWriter out=res.getWriter();
        String url="jdbc:mysql://localhost:3306/employee",username="root";
        String password="root";
        Connection con=null;
        PreparedStatement pst;
        String query="select*from user";
        ResultSet rs;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection(url,username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Entered");
        if (user.getType().equals("add")) {
            try {
                String insertquery="INSERT INTO user" +"  (id,firstname,lastname,  mailid) VALUES " +" (?,?, ?, ?);";
                pst = con.prepareStatement(insertquery);
                pst.setString(1, String.valueOf(user.getId()));
                pst.setString(2, user.getFirstName());
                pst.setString(3, user.getLastName());
                pst.setString(4, user.getMailid());
                
                jsonfile.put("id", String.valueOf(user.getId()));
                jsonfile.put("firstname", user.getFirstName());
                jsonfile.put("lastname", user.getLastName());
                jsonfile.put("mailid", user.getMailid());
                jsonfile.put("status","User Added Successfully");
                
                String final1=gson.toJson(jsonfile);
                System.out.println(final1);
                out.println(final1);
                
                pst.executeUpdate();   
            } catch (Exception e) {
                out.println("error"+e);
            }
        }
        out.flush();
    }
}

基本上,我可以建议您理解CORS是什么。简言之,您正试图提出跨源请求(可能来自
http://localhost:4200
http://localhost:8080
)然后您必须使用CORS,否则浏览器会出于安全原因阻止此操作

然而,这可能不是你想要的。出现此问题的原因是,您运行的是ember开发服务器,因此其来源与后端不同。但是,在以后的生产中,这种情况不会发生-您不会在那里运行ember服务器,但可能有一个Web服务器同时为您的后端和前端服务

对于这种情况(并非总是如此,但经常如此),ember开发服务器具有
--proxy
选项。所以您可以运行
ember-service--proxy=http://localhost:8080
然后它将代理来自
http://localhost:4200
http://localhost:8080

然后更改从
获取的
URL。“http://localhost:8080/UserManagement/UserManagementServlet“
”/UserManagement/UserManagementServlet“
。 这是因为如果您不指定原点,而是以
/
开头,则它始终是当前原点。这也有一个好处,您不必为生产更改此设置

然后浏览器将请求
”http://localhost:4200/UserManagement/UserManagementServlet“
将在没有CORS的情况下工作(也不需要
模式:“无CORS”
),并且ember开发服务器将重定向它

但是,如果您计划在生产中为后端和前端使用单独的服务器,这将不起作用,您需要使用CORS


关于
模式的简要说明:“无cors”
。这将始终阻止您读取响应,从而使请求无法加载数据。这仅与发送数据相关。见:

JavaScript可能无法访问结果响应的任何属性


非常感谢你的帮助!!!!成功了。
//Required Header files
@WebServlet("/UserManagementServlet")
public class UserManagementServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException 
    {   res.setContentType("application/json");  
        res.setCharacterEncoding("UTF-8");
        Gson gson=new Gson();
        BufferedReader br=new BufferedReader(new InputStreamReader(req.getInputStream()));
        String param=br.readLine();
        User user = gson.fromJson(param, User.class);
        HashMap<String,String> jsonfile=new HashMap<String,String>();
        PrintWriter out=res.getWriter();
        String url="jdbc:mysql://localhost:3306/employee",username="root";
        String password="root";
        Connection con=null;
        PreparedStatement pst;
        String query="select*from user";
        ResultSet rs;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection(url,username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Entered");
        if (user.getType().equals("add")) {
            try {
                String insertquery="INSERT INTO user" +"  (id,firstname,lastname,  mailid) VALUES " +" (?,?, ?, ?);";
                pst = con.prepareStatement(insertquery);
                pst.setString(1, String.valueOf(user.getId()));
                pst.setString(2, user.getFirstName());
                pst.setString(3, user.getLastName());
                pst.setString(4, user.getMailid());
                
                jsonfile.put("id", String.valueOf(user.getId()));
                jsonfile.put("firstname", user.getFirstName());
                jsonfile.put("lastname", user.getLastName());
                jsonfile.put("mailid", user.getMailid());
                jsonfile.put("status","User Added Successfully");
                
                String final1=gson.toJson(jsonfile);
                System.out.println(final1);
                out.println(final1);
                
                pst.executeUpdate();   
            } catch (Exception e) {
                out.println("error"+e);
            }
        }
        out.flush();
    }
}
user.js:54 Uncaught (in promise) SyntaxError: Unexpected end of input
    at UserComponent.user (user.js:54)"