简体   繁体   English

如何从ajax的数据成功中选择结果?

[英]How to select the result from data success of ajax?

These are my code: 这些是我的代码:

$("#promo_form").submit(function(stay){
    $.ajax ({
        type: "post",
        url: "<?=base_url()?>promo/code_validate",
        data: $("#promo_form").serialize(),
        success: function(data){
            $("#myModalLabel").html(data);
        }
    });
    stay.preventDefault();
});

And the success data result below: 而成功数据结果如下:

$result1 = "code is invalid"; $ result1 =“代码无效”;

$result2 = "promo unavailable"; $ result2 =“促销不可用”;

How can I select these and retrieve it? 我该如何选择并检索呢?

This is what I did below but now working. 这是我在下面所做的,但现在可以正常工作。

$("#myModalLabel").html(data->result1);

JavaScript uses dot syntax for accessing object properties, so... JavaScript使用点语法来访问对象属性,因此...

$("#myModalLabel").html(data.result1);

I'll also add that you want to make sure the page at promo/code_validate prints out a JSON response, as that is how data will become an object ( $.ajax is intelligent about how to parse the server's response, see the link below). 我还要补充一点,您要确保位于promo/code_validate的页面打印出JSON响应,因为这就是data将如何成为对象的方式( $.ajax关于如何解析服务器的响应很聪明,请参见下面的链接。 )。 So your code_validate page might look something like this: 因此,您的code_validate页面可能如下所示:

{
    "result1": "code is invalid",
    "result2": "promo unavailable"
}

http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/

Vincent, add the dataType paramater to your ajax call, setting it to 'json'. Vincent,将dataType参数添加到您的Ajax调用中,将其设置为“ json”。 Then just parse the response to convert it to an object so that you can access the variables easily, eg 然后只需解析响应即可将其转换为对象,以便您可以轻松访问变量,例如

in AJAX call:
dataType: "json"

in success function:
var obj = jquery.parseJSON(data);
console.log(obj);//echo the object to the console
console.log(obj.result1);//echo the result1 property only, for example

The simplest way to do what you want is create a php associative array of your values then json encode the array and echo the resulting string like this: 做您想要的事情的最简单的方法是创建一个由值组成的php关联数组,然后对数组进行json编码并回显这样的结果字符串:

$response = ["result1"=>"code is invalid", "result2"=>"promo unavailable"];
echo json_encode($response);

Then on the client side, access them like this 然后在客户端上像这样访问它们

$("#promo_form").submit(function(stay){
    $.ajax ({
        type: "post",
        url: "<?=base_url()?>promo/code_validate",
        data: $("#promo_form").serialize(),
        success: function(data){
           $("#modal-1").html(data.result1); 
           $("#modal-2").html(data.result2);
        }
    });
    stay.preventDefault();
});

Do this 做这个

Function controller 功能控制器

function code_validate()
{
    echo json_encode(array('result1'=>'Code is invalid',
                           'result2'=>'Promo not available');
}

Javascript Java脚本

$("#promo_form").submit(function(stay){
    $.ajax ({
        type: "post",
        url: "<?=base_url()?>promo/code_validate",
        data: $("#promo_form").serialize(),
        success: function(data){
            var mydata = JSON.parse(data);
            console.log(mydata.result1);
            console.log(mydata.result1);
        }
    });
    stay.preventDefault();
});

You must return it as JSON. 您必须将其作为JSON返回。

Goodluck meyt 祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM