Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery ajax结果数据_Jquery_Ajax - Fatal编程技术网

jquery ajax结果数据

jquery ajax结果数据,jquery,ajax,Jquery,Ajax,在ajax调用中使用返回数据时遇到问题。最好的办法是什么 我只是将信息发送到checker.jsp。它计算它。如果检查结果良好,它将以绿色响应。如果不好,它会以红色响应 如果我在成功中使用此选项: alert(light_color); 我看到的似乎是整个网页,上面有40行“红色”字样。因此,if和ELSE不起作用。我想使用结果,并且只使用结果。不是html和其中的所有内容 我做错了什么 任何帮助都将不胜感激 $.ajax({ type: 'POST', ur

在ajax调用中使用返回数据时遇到问题。最好的办法是什么

我只是将信息发送到checker.jsp。它计算它。如果检查结果良好,它将以绿色响应。如果不好,它会以红色响应

如果我在成功中使用此选项:

alert(light_color);
我看到的似乎是整个网页,上面有40行“红色”字样。因此,if和ELSE不起作用。我想使用结果,并且只使用结果。不是html和其中的所有内容

我做错了什么

任何帮助都将不胜感激

 $.ajax({
        type: 'POST',
        url: 'checker.jsp',
        data: {
            'bank_cnt': bank_cnt
        },
        success: function (result) {
            var light_color = result;
            if (light_color === 'Red') {
              alert('Red');  
            } else if (report === 'Green') {
                alert('Green');
            } else {
                alert('didnt work');
            }
        }
    });
我想使用结果,并且只使用结果。不是html和其中的所有内容


“结果”包含“html和其中的所有内容”。您需要修改checker.jsp以仅发送“红色”或“绿色”,并在文本响应的标题中设置内容类型。您应该研究如何以JSON格式响应请求。

我想您忘记了阻止默认浏览器操作,请尝试这些行,并向我展示您得到了什么

jQuery(document).ready(function ($) {
    $("#your-form-name").submit(function (event) {
        // Prevent the form from submitting via the browser.
        event.preventDefault();
        // request var definition and filling
        bank_cnt= "SOME_DATA"
        // Call your function
        respViaAjax();
}
function respViaAjax() {
    //the response
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=UTF-8", //Or x-www-form-urlencoded or any other type
        url: "/checker.jsp",
        data: {
            'bank_cnt': bank_cnt
        },
        dataType: 'text',
        timeout: 10000,
        success: function (result) {
            var light_color = result;
            if (light_color === 'Red') {
              alert('Red');  
            } else if (report === 'Green') {
                alert('Green');
            } else {
                alert('didnt work');
            }
        }
        }
    });
});
如有必要,不要忘记更改var名称。希望它对您有用。

1)使用JSON存储您的结果:

<%@page import="org.json.simple.JSONObject"%>
<%
    JSONObject json = new JSONObject(); 

    //Result stored to JSON on the left.
    //Your result w/ Red or Green stored on the right.
    json.put("result", "result");
%>
注意:如果您没有上面声明的
contentType
,您可能需要解析JSON对象:

jQuery.parseJSON(json);
2)如果您不想使用JSON:


将结果作为单独的jsp返回,其中只包含结果文本,并在
url
属性中引用该页面,如Alex所述。抓取jsp将返回整个页面,因此如果您只想要结果,结果必须是页面的全部组成部分。

您是否尝试编写contenttype?是否使用JSON存储结果?对于测试,请尝试在
checker.jsp
中发送一个类似“Green”的常量字符串,而无需任何额外过程。你得到这个字符串了吗?在
success
块的开头,写
alert(result),然后运行,在这种情况下你看到了什么?这是正确的。您可以单独发送颜色结果,并在url属性中引用该结果,或者如您所说,以JSON格式响应。JSON响应是我选择提供的答案。这两种解决方案都应该有效+1.
jQuery.parseJSON(json);