如何从java中一个ajax成功函数的单个值响应函数、java、ajax

由网友(不能说的秘密)分享简介:我有我的 AJAX 调用是这样的:I have my ajax call like this:var x = document.forms["myForm"]["param1"].value;var y = document.forms["myForm"]["param2"].value;$.ajax({url...

我有我的 AJAX 调用是这样的:

I have my ajax call like this:

var x = document.forms["myForm"]["param1"].value;
var y = document.forms["myForm"]["param2"].value;
$.ajax({
        url: "./webapi/user/get?param1="+x+'&param2='+y,
        contentType: 'application/json',
        type: "GET",
        data:{get_param:'value'},
        success:function(data)
        {
            alert(data);
        }
        error:function(x,y,z){
            alert(x+" "+y+" "+z);
        }
    });

我在 UserService的 类我的Java 功能是这样写的:

I have my java function in UserService class is written like this:

public int getFlag(String p_param1,String p_param2) {
    int flag=0;
    try{
        dcc.connectionCall();
        stmt=dcc.con.prepareCall("{? = call function1(?,?)}");
        stmt.registerOutParameter(1,OracleTypes.INTEGER);
        stmt.setString(2, p_param1);
        stmt.setString(3, p_param2);
        stmt.execute();
        flag = stmt.getInt(1);
        System.out.println("FLAG = "+flag+" "+p_param1+" "+p_param2);
    }
    catch(SQLException e){
        e.printStackTrace();
    }
    return flag;
}

我有我的 UserResource 具有以下功能:

userService userservice = new UserService();
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public int getUser(@QueryParam("param1") String p_param1,@QueryParam("param2") String p_param2) throws SQLException{
    int flag = userservice.getUserFlag(p_param1,p_param2);
    System.out.println("FLAG= "+flag+" "+p_param1+" "+p_param2);
    return flag;
}

上面的东西给了Eclipse的控制台进行适当的输出。但我无法得到 AJAX 通话,这无非是标志的响应。如何获得呢?我搜索了很多,但一切都像得到对象作为回应,并把它字符串化有。但整个 JSON 是不是需要我。我只是需要的标志。在这么多的AP $ P $任何帮助pciated。我使用的Tomcat 作为服务器和上面的分配是 ConnectionPooling REST风格的 Web服务。

The above thing gives proper output on eclipse console. However I am unable to get the response in ajax call, which is nothing but the flag. How do I get it? I searched a lot but everything is like getting Object as a response and get it stringified there. But entire json is not needed to me. I just need the flag. Any help upon this much appreciated. I am using Tomcat as a server and the above assignment is for ConnectionPooling with RESTful web service.

推荐答案

使用 $。parseJSON(数据),并得到你想要的。或者,你将不得不改变你的返回类型,以明文(在服务器端),这似乎并不可行。

Use $.parseJSON(data) and get what you want. Or you will have to change your return type to cleartext (at your server-side) which does not seem feasible.

@Altinak是正确的,指的是 jquery.ajax 文档:

@Altinak is right, referring to jquery.ajax documentation:

不同类型的反应$。阿贾克斯()调用中受到   不同种$ P $对 - 处理被传递到成功之前   处理程序。于$ P $对 - 处理的类型取决于默认   内容类型的响应,但可明确设置使用   dataType的选项。如果提供了的dataType选项,内容类型   响应头将被忽略。

Different types of response to $.ajax() call are subjected to different kinds of pre-processing before being passed to the success handler. The type of pre-processing depends by default upon the Content-Type of the response, but can be set explicitly using the dataType option. If the dataType option is provided, the Content-Type header of the response will be disregarded.

阅读全文

相关推荐

最新文章