简体   繁体   English

如何从Java Rest Web服务返回ajax中的值?

[英]How to return a value in ajax from a Java Rest Web service?

I have a web application with HTML and JQuery, in which it has a login form. 我有一个带有HTML和JQuery的Web应用程序,其中有一个登录表单。 When I click the submit button I am using ajax to send the data using Post request to a web service. 当我单击提交按钮时,我使用ajax使用Post请求将数据发送到Web服务。 The web service is built with Java. Web服务使用Java构建。

Here is my html form: 这是我的html表单:

<form id="mdxLogin" action="" method="post">
    <div class="ui-content">
            <p><input type="email" id="mdxEmail" name="mdxEmail" class="ui-field-contain" value="" placeholder="MDX Email" /> </p>
            <p><input type="password" id="mdxPassword" name="mdxPassword" class="ui-field-contain" value="" placeholder="MDX Password" /></p>
    </div>
    <div class="ui-content">
            <input type="submit" class="ui-field-contain" value="Login" id="sub"/>
    </div>
</form>

The below is my Ajax code to post to my web service 以下是我发布到我的Web服务的Ajax代码

$("#mdxLogin").submit(function(e) {
    e.preventDefault();
    var mdxEmail = $("input[name=\"mdxEmail\"]").val();
    var mdxPassword = $("input[name=\"mdxPassword\"]").val();

    $.ajax({
        type: "POST",
        url:"http://localhost:8080/RestService/rest/loginService/login",
        dataType:"json",
        data: $("#mdxLogin").serialize(),
        success: function(data, textStatus, jqXHR) {
            // handle your successful response here
            alert(data);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            // handle your fail response here
            alert("Error");
        }
    }); 

})

And the below is the method in my web service 以下是我的Web服务中的方法

@POST
@Path("/login")
@Produces(MediaType.TEXT_PLAIN)
public String login(@FormParam("mdxEmail") String mdxEmail, @FormParam("mdxPassword") String mdxPassword) {
  System.out.println(mdxEmail);
  DBStudent s = new DBStudent();
  String url = null;

  if (s.checkLogin(mdxEmail, mdxPassword)) {
      url = s.getCalendar(mdxEmail, mdxPassword);
  }

  return url;
 }

So far what I managed to do is to post the data to my web service but didn't get any response. 到目前为止,我设法做的是将数据发布到我的Web服务但没有得到任何响应。 My question is how can I access the returned url from my web service with Ajax? 我的问题是如何使用Ajax从我的Web服务访问返回的URL?

In your code I spot one error which should prevent it to work properly. 在你的代码中我发现了一个错误,它应该阻止它正常工作。 In the client side you're saying to jQuery that the expected type is JSON, but the server produces a string, in your case a URL, which is not JSON. 在客户端,你对jQuery说,期望的类型是JSON,但服务器产生一个字符串,在你的情况下是一个URL,而不是JSON。 Therefore jQuery when tries to parse the data received fails because it's not JSON data. 因此,jQuery在尝试解析收到的数据时失败,因为它不是JSON数据。 This mistake should trigger the jQuery error block. 这个错误应该触发jQuery错误块。

Apply just this change in the client: 在客户端中仅应用此更改:

dataType:"text"

If you want to test your code without worrying about the Same Origin Policy you can disable it, look at these threads 如果您想测试代码而不必担心同源策略,可以禁用它,查看这些线程

Disable firefox same origin policy 禁用firefox同源策略

Disable same origin policy in Chrome 在Chrome中停用相同的来源政策

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

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