angularjs 应用程序的网络代理

web-proxy for angularjs application

我必须为我的 angularjs 文件创建一个网络代理脚本,因为我收到了 CORS(跨源请求方法)的错误,而且我没有任何选项来使用访问控制允许源,因为我不能对我的服务器端进行任何更改。 我的后端数据在 java 中。所以请有人告诉我如何为我的 angularjs 应用程序制作网络代理。

或者有什么方法可以绕过浏览器的 cors 请求。

用户 json_decode 具有 true 参数

$data = "{"studentid":"5","firstame":"jagdjasgd","lastname":"kjdgakjd","email":"dgahsdg@em.com"}";
$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": "david123@gmail.com",
        "email": "david123@gmail.com",
        "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": "chrisgayle@email.com",
        "email": "chrisgayle@email.com",
        "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": "dgahsdg@em.com",
        "email": "dgahsdg@em.com",
        "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

使用 foreach 和 json_decode 的快速解决方法。

如果您的 print_r($json) 采用这种格式:

Array
(
    [0] => Array
        (
            [studentid] => 5
            [firstame] => jagdjasgd
            [lastname] => kjdgakjd
            [gender] => 1
            [email] => dgahsdg@em.com
            [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
}

希望这会有所帮助。

像下面这样解码你的数组..

$newarr= json_decode('urjsonstring');

extract($newarr);

$query="insert into stud values($studentid, $firstname,$lastname...)";