0

我是第一次尝试 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());
    }
}

爆破!!我知道我的大部分问题是代码!该死的已经发布了!

4

2 回答 2

0

Thinkdata需要是一个数组。

var usernameVal = $("#textbox").val;
 $.ajax({
            type: 'POST',
            url: '/FirstProject/src/Resource/resource/welcome',
            data: { username : usernameVal }
于 2013-10-09T11:18:45.187 回答
0

将数据作为带有索引的 json 发送,就像{"username":username }在 ajax 数据中一样 ....,data: {"username":username },....

于 2013-10-09T11:19:10.427 回答