如何将布尔结果发送给Ajax调用 [英] How to send boolean result to ajax call

查看:45
本文介绍了如何将布尔结果发送给Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ajax调用中发送布尔结果.

I want to send Boolean result in ajax call.

Ajax呼叫

function myMethod() {
		 
    $.ajax({
        type : "GET",
	url : myurl
	dataType : "json",
	contentType: "application/json",
	crossDomain: true,
	success : function(data) {
            alert("success :"+data);
			
	},
	error : function(data) {
   	    alert("Fialed to get the data");
	}
    });
	
}

我要从中发送布尔结果的控制器方法.

@RequestMapping(value = "/myMethod", method = RequestMethod.GET)
@ResponseBody
public boolean myMethod(empId) {
    flag = false;
		
    try {
    	if (empId != null)
            newName = Employee.getName(empId);
        else
            newName = Employee.getName();
    } catch (Exception e1) {
        flag = true;
        System.out.println("flag :" + flag);
        e1.printStackTrace();
    }
		
    return flag;
}

我想将布尔标志结果发送给ajax调用.我该如何发送.不要介意逻辑..我只想知道如何将布尔结果发送给ajax调用.请帮忙.

i want to send the Boolean flag result to ajax call. How do i send it. dont mind about the logic .. I just want to know how can i send boolean result to ajax call. Please help .

推荐答案

Ajax使用HTTP,它是一种文本协议,没有布尔值的概念.但是,您可以返回字符串"1"或"0"来表示您的布尔值.

Ajax uses HTTP which is a text protocol and has no concept of booleans. However, you can return the strings "1" or "0" to represent your boolean values.

然后,在您的成功"回调中:

Then, in your "success" callback:

success : function ( data ) {
    if ( data * 1 ) {
        // true result
    } else {
        // false result
    }
}

这篇关于如何将布尔结果发送给Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆