我必须为我的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数据。 –
是的。 $文件是您的数据。 –