简体   繁体   English

使用 axios 帖子和 php 发送花括号数组

[英]Sending array of curly braces with axios post and php

i am trying to send params via axios.post then retrieve the data with PHP.我正在尝试通过 axios.post 发送参数,然后使用 PHP 检索数据。

this is the axios code:这是 axios 代码:

axios.post(""+ALL.API_URL+"/sellwithus/set.php", final)
        .then(function (response) {
            // handle success
            console.log(response);
           
            
          })
          .catch(function (error) {
            // handle error
            alert(error.message);
          })
          .finally(function () {
            // always executed
            //alert('Finally called');
            //console.log(jsonData);
          });

this is the arrays code:这是 arrays 代码:

var toMerge =[firstData,secondData,votes];
    var finalArrayToPhp = [].concat.apply([], toMerge);
    var str = JSON.stringify(finalArrayToPhp)
    var final = str.replace(/[{}]/g, "").replace(/\[/g, '{').replace(/]/g, '}');
console.log(final);
//{"storename":"Mikha store","storeaddress":"Mielya","ownername":"Mikha matta","ownerphone":555555555,"businnesstype":1,"businnessbranchesaddresses":{"Mielya"},"businnesslicense":"A24324","deliveryservices":1,"minprice4delivery":"10","tablet":1,"businnesscats":{"item":"Juventus","id":"JUVE"},"tablet":1,"businnesscats":{"item":"Juventus","id":"JUVE"}}

console.log(toMerge);
//[[{"storename": "Mikha store"}, {"storeaddress": "Mielya"}, {"ownername": "Mikha matta"}, {"ownerphone": 555555555}, {"businnesstype": 1}], [{"businnessbranchesaddresses": [Array]}, {"businnesslicense": "A24324"}, {"deliveryservices": 1}, {"minprice4delivery": "10"}], [{"tablet": 1}, {"businnesscats": [Array]}]]

console.log(str);
//[{"storename":"Mikha store"},{"storeaddress":"Mielya"},{"ownername":"Mikha matta"},{"ownerphone":555555555},{"businnesstype":1},{"businnessbranchesaddresses":["Mielya"]},{"businnesslicense":"A24324"},{"deliveryservices":1},{"minprice4delivery":"10"},{"tablet":1},{"businnesscats":[{"item":"Juventus","id":"JUVE"}]}]

this is my php code:这是我的 php 代码:

<?php
$_POST = json_decode(file_get_contents("php://input"),true);



if(isset($_POST['ownername'])){
    $myObj->theans = "yes";
}else{
    $myObj->theans = "no";
}

$myJSON = json_encode($myObj);
echo $myJSON;
?>

this is my response:这是我的回应:

{"config": {"adapter": [Function xhrAdapter], "data": "{\"storename\":\"Mikha store\",\"storeaddress\":\"Mielya\",\"ownername\":\"Mikha matta\",\"ownerphone\":555555555,\"businnesstype\":1,\"businnessbranchesaddresses\":{\"Mielya\"},\"businnesslicense\":\"A118\",\"deliveryservices\":1,\"minprice4delivery\":\"10\",\"tablet\":1,\"businnesscats\":{\"item\":\"Juventus\",\"id\":\"JUVE\"}}", "headers": {"Accept": "application/json, text/plain, */*", "Content-Type": "application/x-www-form-urlencoded"}, "maxBodyLength": -1, "maxContentLength": -1, "method": "post", "timeout": 0, "transformRequest": [[Function transformRequest]], "transformResponse": [[Function transformResponse]], "transitional": {"clarifyTimeoutError": false, "forcedJSONParsing": true, "silentJSONParsing": true}, "url": "http://45.79.250.188/~penaco/jelivery/sellwithus/set.php", "validateStatus": [Function validateStatus], "xsrfCookieName": "XSRF-TOKEN", "xsrfHeaderName": "X-XSRF-TOKEN"}, "data": {"theans": "no"}, "headers": {"connection": "Upgrade, close", "content-encoding": "gzip", "content-type": "text/html; charset=UTF-8", "date": "Mon, 31 Jan 2022 21:40:29 GMT", "server": "Apache", "transfer-encoding": "Identity", "upgrade": "h2,h2c", "vary": "Accept-Encoding"}, "request": {"DONE": 4, "HEADERS_RECEIVED": 2, "LOADING": 3, "OPENED": 1, "UNSENT": 0, "_aborted": false, "_cachedResponse": undefined, "_hasError": false, "_headers": {"accept": "application/json, text/plain, */*", "content-type": "application/x-www-form-urlencoded"}, "_incrementalEvents": false, "_lowerCaseResponseHeaders": {"connection": "Upgrade, close", "content-encoding": "gzip", "content-type": "text/html; charset=UTF-8", "date": "Mon, 31 Jan 2022 21:40:29 GMT", "server": "Apache", "transfer-encoding": "Identity", "upgrade": "h2,h2c", "vary": "Accept-Encoding"}, "_method": "POST", "_perfKey": "network_XMLHttpRequest_http://45.79.250.188/~penaco/jelivery/sellwithus/set.php", "_performanceLogger": {"_closed": false, "_extras": [Object], "_pointExtras": [Object], "_points": [Object], "_timespans": [Object]}, "_requestId": null, "_response": "{\"theans\":\"no\"}", "_responseType": "", "_sent": true, "_subscriptions": [], "_timedOut": false, "_trackingName": "unknown", "_url": "http://45.79.250.188/~penaco/jelivery/sellwithus/set.php", "readyState": 4, "responseHeaders": {"Connection": "Upgrade, close", "Content-Encoding": "gzip", "Content-Type": "text/html; charset=UTF-8", "Date": "Mon, 31 Jan 2022 21:40:29 GMT", "Server": "Apache", "Transfer-Encoding": "Identity", "Upgrade": "h2,h2c", "Vary": "Accept-Encoding"}, "responseURL": "http://45.79.250.188/~penaco/jelivery/sellwithus/set.php", "status": 200, "timeout": 0, "upload": {}, "withCredentials": true}, "status": 200, "statusText": undefined}

problem: i get theans = no.问题:我得到了theans = no。

any suggestions please?请问有什么建议吗?

ok, my solution was like this:好的,我的解决方案是这样的:

in axios.post i sent the finalArrayToPhp as data:在 axios.post 我发送了 finalArrayToPhp 作为数据:

axios.post(""+ALL.API_URL+"/sellwithus/set.php", {data:finalArrayToPhp})

in php:在 php 中:

$dataArr = array();
foreach($_POST["data"] as $first):
    foreach($first as $v => $second):
        $dataArr[$v]=$second; 
    endforeach;
endforeach;

thanks!谢谢!

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

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