我必須爲我的angularjs文件創建一個web代理腳本,因爲我得到了CORS(交叉源請求方法)的錯誤,而且我沒有任何選項可以使用訪問控制允許源,因爲我不能對我的服務器端做任何更改。 我的後端數據是用java編寫的。所以請有人告訴我如何爲我的angularjs應用程序創建一個web代理。angularjs應用程序的web代理
或者無論如何繞過我的瀏覽器cors請求。
我必須爲我的angularjs文件創建一個web代理腳本,因爲我得到了CORS(交叉源請求方法)的錯誤,而且我沒有任何選項可以使用訪問控制允許源,因爲我不能對我的服務器端做任何更改。 我的後端數據是用java編寫的。所以請有人告訴我如何爲我的angularjs應用程序創建一個web代理。angularjs應用程序的web代理
或者無論如何繞過我的瀏覽器cors請求。
快速解決使用foreach和json_decode的問題。
如果您的print_r($ JSON)自帶的格式如下:
Array
(
[0] => Array
(
[studentid] => 5
[firstame] => jagdjasgd
[lastname] => kjdgakjd
[gender] => 1
[email] => [email protected]
[fathername] => hashsdh
[mothername] => djhavshd
[birthday] => 2016-03-21
[address] => gafdhfadhs
[tenth] => 45.235
[twelfth] => 56.25
)
)
這將這樣的伎倆:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$json = json_decode(file_get_contents("php://input"), true);
//print_r($json);
$data =array();//Open blank array for student data
$num = array();//Open Blank array for number of student
foreach($json as $k => $v):
$num [] = $v; //number of student
if(is_array($v)){
foreach($v as $key=>$val):
$data[$key] = $val;//Student data
endforeach;
}
endforeach;
$row= count($num);//Put number of student in $row
for($i=1; $i<=$row; $i++){
$q = 'INSERT INTO table (`col1`)
VALUES($data['studentid'])';//Looping through sql statement
}
希望這會有所幫助。
用戶json_decode
與true
PARAMS
$data = "{"studentid":"5","firstame":"jagdjasgd","lastname":"kjdgakjd","email":"[email protected]"}";
$d = json_decode($data,true); // true means it will result in aaray
print_r($d);
$stdId = $d['studentid'];
$fname = $d['firstname'];
$lname = $d['lastname'];
$mail = $d['email'];
編輯: 對於多個JSON數據:
$data = '[
{
"0": "1",
"studentid": "1",
"1": "David",
"firstname": "David",
"2": "Beckham",
"lastname": "Beckham",
"3": "1",
"gender": "1",
"4": "[email protected]",
"email": "[email protected]",
"5": "Beckham",
"fathername": "Beckham",
"6": "Beckhamii",
"mothername": "Beckhamii",
"7": "2016-03-13",
"birthday": "2016-03-13",
"8": "dgasdhghasd\nkajsdgjaksdh\nkahdgjaksgdas",
"address": "dgasdhghasd\nkajsdgjaksdh\nkahdgjaksgdas",
"9": "58.25",
"tenth": "58.25",
"10": "62.25",
"twelfth": "62.25"
},
{
"0": "3",
"studentid": "3",
"1": "Chris",
"firstname": "Chris",
"2": "Gayle",
"lastname": "Gayle",
"3": "1",
"gender": "1",
"4": "[email protected]",
"email": "[email protected]",
"5": "Chris Potters",
"fathername": "Chris Potters",
"6": "Christine",
"mothername": "Christine",
"7": "2016-04-20",
"birthday": "2016-04-20",
"8": "adhafsdh\njgadahksgdkjas\njagdjahsdlkajsld\nkajsgdjlahsdlkas",
"address": "adhafsdh\njgadahksgdkjas\njagdjahsdlkajsld\nkajsgdjlahsdlkas",
"9": "87.587",
"tenth": "87.587",
"10": "98.256",
"twelfth": "98.256"
},
{
"0": "5",
"studentid": "5",
"1": "jagdjasgd",
"firstname": "jagdjasgd",
"2": "kjdgakjd",
"lastname": "kjdgakjd",
"3": "1",
"gender": "1",
"4": "[email protected]",
"email": "[email protected]",
"5": "hashsdh",
"fathername": "hashsdh",
"6": "djhavshd",
"mothername": "djhavshd",
"7": "2016-03-21",
"birthday": "2016-03-21",
"8": "gafdhfadhs\nagdkjashdas\ndjkahsdklsaj",
"address": "gafdhfadhs\nagdkjashdas\ndjkahsdklsaj",
"9": "45.235",
"tenth": "45.235",
"10": "56.25",
"twelfth": "56.25"
}
]';
$json = json_decode($data, true);
echo '<pre>';
foreach ($json as $key => $value) {
echo "StudentID: ".$value['studentid']."<br>";
}
輸出:
StudentID: 1
StudentID: 3
StudentID: 5
什麼,如果我有ňstudentid的數量。我怎麼寫這個$數據。 –
@RamansathiyaNarayanan然後用於每個循環 –
好吧,這個數據的任何例子,因爲我新來這條線。 我的表單包含完全數據,當我點擊從我的html頁面提交時,這些數據必須去mysql數據庫。任何例子請 –
解碼烏爾陣列像下面..
$newarr= json_decode('urjsonstring');
extract($newarr);
$query="insert into stud values($studentid, $firstname,$lastname...)";
給這個輸出 警告:在C的foreach()提供參數無效:\ XAMPP \ htdocs中\所以\ test.php的上線路20 陣列 ( ) 注意:未定義偏移量:0在C:\ xampp \ htdocs \ so \ test.php 25行 –
你能告訴我$文件是來自post方法的我的json數據。 –
是的。 $文件是您的數據。 –