对Jersey WebService的AJAX调用不起作用

时间:2013-10-09 11:14:11

标签: java jquery ajax web-services

我第一次尝试使用AJAX。我正在使用Jersey Web Service作为被调用的东西。但我的电话总是执行错误部分。救命!请

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Jquery Basic</title>

<link rel="stylesheet" type="text/css" href="mystyle.css">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

<script>
$(document).ready(function(){
    $("#submit1").click(function() {
        alert("click");
        var username = $("#textbox").val;
        $("#para1").text(username);
        $.ajax({
            type: 'POST',
            url: '/FirstProject/src/Resource/resource/welcome',
            data: username,

            success:  function(){alert("Login Success!")},
            error: function(){alert("Login Failure!")}
               });                    
        alert("ajax passed");
       });
    });
</script>
</head>

<body>
<a id="body1">JQuery Test Page</a><br>
<div id="heading"><a>Enter Your Details</a></div>
<div>
<div id="heading1"><a>UserName:</a></div>
<div><input id="textbox" type="text"/></div>
<button id="submit1">Submit</button>
</div>
<div><p id="para1"></p></div>
</body>
</html>

WebService如下

package Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

import POJO.passwordPojo;
import POJO.usernamePojo;



public class resource {

@POST
@Path("welcome")
public String welcomeFunction(@FormParam("username") String username)
    {
        setUserNameData(username);

        return "success";
    }

usernamePojo userName = new usernamePojo();
passwordPojo password = new passwordPojo();

public void setUserNameData(String userNameData)
    {
        userName.setUserName(userNameData.toString());
        printuserName();
    }
public void setpasswordData(String passwordData)
    {
        password.setPassword(passwordData.toString());
        printPassword();
    }
public void printuserName()
    {
        System.out.println("UserName:"+userName.getUserName());
    }
public void printPassword()
    {
        System.out.println("Password"+password.getPassword());
    }
}

高炉!!我知道我的大部分问题都是代码!!血腥的发布了!

2 个答案:

答案 0 :(得分:0)

认为data需要是一个数组。

var usernameVal = $("#textbox").val;
 $.ajax({
            type: 'POST',
            url: '/FirstProject/src/Resource/resource/welcome',
            data: { username : usernameVal }

答案 1 :(得分:0)

在ajax数据中发送数据为json,索引类似{"username":username }  ....,data: {"username":username },....