Jquery Ajax成功函数基于数组响应

时间:2012-03-01 21:31:52

标签: php jquery ajax codeigniter codeigniter-2

我有一个表单,它通过Jquery Ajax Call将数据发送到控制器函数,该函数处理数据,然后从第三方API获取响应。

数据通过POST发送到控制器,然后这些数据通过Curl发送到第三方API,然后以字符串形式返回,然后转换为数组,最后进行json编码。

我遇到了能够使用“echo json_encode($ array);”发回数组的地方,但我似乎无法弄清楚如何使用数组。

我想要最终做的是,如果数组上的“结果”键=“失败”,那么我会向div添加一条消息,如果它等于“result”=“success”,那么我会追加消息并运行第二个函数。

返回的响应数组是: 的 {"transaction_id":"8267_03-01-12_16:02:58_0","action":"payment","result":"failed","errors":"98","errors_meaning":"(98)","customer_errors_meaning":"","processing_time":"0.0117"}

继承我的代码

表格& Jquery代码:

<div class="creditcard">
    <form method="post">
        <strong>Credit Card Type:</strong>
        <select name="cc_type" id="cc_type" size="1">
            <option value="VISA">Visa</option>
            <option value="MASTERCARD">MasterCard</option>
        </select>

        <strong>Credit Card Number:</strong>
        <input type="text" name="cc_number" id="cc_number" size="24" value="">

        <strong>Credit Card Expiration Date:</strong>
        <select name="cc_expdate_month" id="cc_expdate_month">
            <option value="01" >01</option>
            <option value="02" >02</option>
            <option value="03" >03</option>
            <option value="04" >04</option>
            <option value="05" >05</option>
            <option value="06" >06</option>
            <option value="07" >07</option>
            <option value="08" >08</option>
            <option value="09" >09</option>
            <option value="10" >10</option>
            <option value="11" >11</option>
            <option value="12" >12</option>
        </select> / <select name="cc_expdate_year" id="cc_expdate_year">
            <option value="06" >2006</option>
            <option value="07" >2007</option>
            <option value="08" >2008</option>
            <option value="09" >2009</option>
            <option value="10" >2010</option>
            <option value="11" >2011</option>
            <option value="12" >2012</option>
            <option value="13" >2013</option>
            <option value="14" >2014</option>
            <option value="15" >2015</option>
            <option value="16" >2016</option>
        </select>

        <strong>Credit Card Security Code:</strong>
        <input type="text" name="cc_security_code" id="cc_security_code" size="4" maxlength="4" value="">
        <input type="hidden" name="business" id="business" value="xxxx@xxxx.xxx" />
        <input type="hidden" name="action" id="action" value="payment" />
        <input type="hidden" name="vericode" id="vericode" value="xxxxxxxx" />

        <input type="hidden" name="first_name" id="first_name" value="testname" />
        <input type="hidden" name="last_name" id="last_name" value="testlname" />

        <input type="hidden" name="address" id="address" value="testaddress" />
        <input type="hidden" name="city" id="city" value="testcity" />
        <input type="hidden" name="state_or_province" id="state_or_province" value="AL" />
        <input type="hidden" name="zip_or_postal_code" id="zip_or_postal_code" value="12345" />
        <input type="hidden" name="country" id="country" value="US" />
        <input type="hidden" name="shipping_address" id="shipping_address" value="testaddress" />
        <input type="hidden" name="shipping_city" id="shipping_city" value="testcity" />
        <input type="hidden" name="shipping_state_or_province" id="shipping_state_or_province" value="AL" />
        <input type="hidden" name="shipping_zip_or_postal_code" id="shipping_zip_or_postal_code" value="12345" />
        <input type="hidden" name="shipping_country" id="shipping_country" value="US" />
        <input type="hidden" name="phone" id="phone" value="1234567890" />
        <input type="hidden" name="email" id="email" value="email@wemail.es" />

        <input type="hidden" name="item_name" id="item_name" value="xxxx.xxx" />
        <input type="hidden" name="item_code" id="item_code" value="Product Name 3," />
        <input type="hidden" name="quantity" id="quantity" value="1" />
        <input type="hidden" name="amount" id="amount" value="205.00" />
        <input type="hidden" name="transaction_id" id="transaction_id" value="1060" />
        <input type="hidden" name="shipment" id="shipment" value="yes" />
        <input type="hidden" name="shipment_method" id="shipment_method" value="USPS Priority Mail" />
        <input type="hidden" name="ip_address" id="ip_address" value="123.45.67.89" />
        <input type="hidden" name="test_php" value="1" />

        <input type="submit" value="Authorize Credit Card" name="authorize_cc" id="authorize_cc" />
        <div class="authorization_result"></div>
    </form>
</div>
<script defer="defer" type="text/javascript">
$(document).ready(function() {

$('#authorize_cc').click(function() {
    var postData = {
        'authorize' : 1 ,
        'cc_type' : $("#cc_type").val(),
        'cc_number' : $("#cc_number").val(),
        'cc_expdate_month' : $("#cc_expdate_month").val(),
        'cc_expdate_year' : $("#cc_expdate_year").val(),
        'cc_security_code' : $("#cc_security_code").val(),
        'business' : $("#business").val(),
        'action' : $("#action").val(),
        'vericode' : $("#vericode").val(),
        'first_name' : $("#first_name").val(),
        'last_name' : $("#last_name").val(),
        'address' : $("#address").val(),
        'city' : $("#city").val(),
        'state_or_province' : $("#state_or_province").val(),
        'zip_or_postal_code' : $("#zip_or_postal_code").val(),
        'country' : $("#country").val(),
        'shipping_address' : $("#shipping_address").val(),
        'shipping_city' : $("#shipping_city").val(),
        'shipping_state_or_province' : $("#shipping_state_or_province").val(),
        'shipping_zip_or_postal_code' : $("#shipping_zip_or_postal_code").val(),
        'shipping_country' : $("#shipping_country").val(),
        'phone' : $("#phone").val(),
        'email' : $("#email").val(),
        'item_name' : $("#item_name").val(),
        'item_code' : $("#item_code").val(),
        'quantity' : $("#quantity").val(),
        'amount' : $("#amount").val(),
        'transaction_id' : $("#transaction_id").val(),
        'shipment' : $("#shipment").val(),
        'shipment_method' : $("#shipment_method").val(),
        'ip_address' : $("#ip_address").val(),
    };

    $.ajax({
            url: "<?php echo base_url().'admin/creditcard/authorize';?>",
            type:'POST',
            data: postData,
            dataType: 'json',
            success: function(output_string){
                $(".authorization_result").append(output_string);

            }
            }); // End of ajax call
    return false;       
}); 
});

</script>

控制器功能

function authorize(){
// Get variables from POST array
$post_str = "action=payment&business="      .urlencode($this->input->post('business'))
            ."&vericode="                   .urlencode($this->input->post('vericode'))
            ."&item_name="                  .urlencode($this->input->post('item_name'))
            ."&item_code="                  .urlencode($this->input->post('item_code'))
            ."&quantity="                   .urlencode($this->input->post('quantity'))
            ."&amount="                     .urlencode($this->input->post('amount'))
            ."&cc_type="                    .urlencode($this->input->post('cc_type'))
            ."&cc_number="                  .urlencode($this->input->post('cc_number'))
            ."&cc_expdate="                 .urlencode($this->input->post('cc_expdate_year')).urlencode($this->input->post('cc_expdate_month'))
            ."&cc_security_code="           .urlencode($this->input->post('cc_security_code'))
            ."&shipment="                   .urlencode($this->input->post('shipment'))
            ."&first_name="                 .urlencode($this->input->post('first_name'))
            ."&last_name="                  .urlencode($this->input->post('last_name'))
            ."&address="                    .urlencode($this->input->post('address'))
            ."&city="                       .urlencode($this->input->post('city'))
            ."&state_or_province="          .urlencode($this->input->post('state_or_province'))
            ."&zip_or_postal_code="         .urlencode($this->input->post('zip_or_postal_code'))
            ."&country="                    .urlencode($this->input->post('country'))
            ."&shipping_address="           .urlencode($this->input->post('shipping_address'))
            ."&shipping_city="              .urlencode($this->input->post('shipping_city'))
            ."&shipping_state_or_province=" .urlencode($this->input->post('shipping_state_or_province'))
            ."&shipping_zip_or_postal_code=".urlencode($this->input->post('shipping_zip_or_postal_code'))
            ."&shipping_country="           .urlencode($this->input->post('shipping_country'))
            ."&phone="                      .urlencode($this->input->post('phone'))
            ."&email="                      .urlencode($this->input->post('email'))
            ."&ip_address="                 .urlencode($this->input->post('ip_address'))
            ."&website_unique_id="          .urlencode($this->input->post('website_unique_id'));

            // Send URL string via CURL
            $backendUrl = "https://www.veripayment.com/integration/index.php";
            $this->curl->create($backendUrl);
            $this->curl->post($post_str);
            $return_str = $this->curl->execute();

            //-------------------------------------
            // Convert String to array
            //-------------------------------------

            $result = array();
            // Explode array where blanks are found
            $resparray = explode(' ', $return_str);

            if ($resparray)
            {
              // save results into an array
              foreach ($resparray as $resp) {
                $keyvalue = explode('=', $resp);
                if(isset($keyvalue[1])){
                $result[$keyvalue[0]] =  str_replace('"', '', $keyvalue[1]);
                }
              }
            }

            //echo $result array
            echo json_encode($result);
}

1 个答案:

答案 0 :(得分:3)

有条理地查看您的回复。

    $.ajax({
        url: "<?php echo base_url().'admin/creditcard/authorize';?>",
        type:'POST',
        data: postData,
        dataType: 'json',
        success: function(output_string){
            if(x = $.parseJSON(output_string)){
              if(x.property == 'true'){ //or  if(x.property != null){
                  $(".authorization_result").append(x);
              }else{
                 $(".authorization_result").append("fail");
               }
            }else{
                 $(".authorization_result").append("fail");
            }
        }
    });