在php变量中设置ajax响应

时间:2019-11-30 13:34:00

标签: php ajax

我试图在php变量中设置json响应。成功响应后,我的html数据打印在div中。 任何想法如何将其上传到Php变量中。可以将其存储在会话或cookie中。

$('#pincode').on('change', function() {
    //const formData = $('#formid').serializeArray();
    var address = $("input[name=address]").val();
    var pincode= $("input[name=pincode]").val();
    var price = $("input[name=price]").val();
    var city = $("input[name=city]").val();
    var country =$('#country').val();
    var state = $('#state').val();
    //var state =$(this).val();
    //console.log(formData);

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $.ajax({
        type:'POST',
        data:{address:address, pincode:pincode, price:price,country:country,state:state,city:city},
        url:'./ava',
        dataType: "text",
        success:function(data){
            $('#gettax').html(data);
            //alert(res);
        },
        error: function (err) {
            console.log(err);
            alert("Something Went Wrong, Please check again");
        }
    });
});

这里是mY控制器,我正在返回计划文本值。并尝试将其恢复为php变量

public function avatax(Request $request){
    $input = $request->all();
    $address=$input['address'];
    $pincode=$input['pincode'];
    $country=$input['country'];
    $state=$input['state'];
    $city=$input['city'];
    $price=$input['price'];
    // print_r(json_decode(json_encode($input)));
    // die();
    //return response()->json(['success'=>$input]);

    $client = new Avalara\AvaTaxClient('phpTestApp', '1.0', 'localhost', 'sandbox');
    $client->withSecurity('ABC', 'ABC');
    // If I am debugging, I can call 'Ping' to see if I am connected to the server
    $p = $client->ping();
    echo('<h2>Ping</h2>');
    echo('<pre>' . json_encode($p, JSON_PRETTY_PRINT) . '</pre>');
    if ($p->authenticated == true) {
        echo '<p>Authenticated!</p>';
    }

    // Create a simple transaction for $100 using the fluent transaction builder
    $tb = new Avalara\TransactionBuilder($client, "ABC", Avalara\DocumentType::C_SALESINVOICE, 'ABC');
    $t = $tb->withAddress('SingleLocation',$address,null,null, $city,$state,$pincode, $country)
        ->withLine($price, 1, null, "P0000000")
        ->create();

    // echo('<h2>Transaction #1</h2>');
    // echo('<pre>' . json_encode($t, JSON_PRETTY_PRINT) . '</pre>');
    // echo '<pre>';print_r($t);

    $ta=$t->totalTax;

    // die();
    return response($ta)
        ->header('Content-Type', 'text/plain');

0 个答案:

没有答案