angularjs应用程序的web-proxy

时间:2016-04-01 10:48:50

标签: angularjs webproxy

我必须为我的angularjs文件创建一个web-proxy脚本,因为我收到了CORS(Cross Origin Request Method)的错误,我没有任何选项来使用Access Control Allow Origin,因为我无法对我的服务器进行任何更改结束。 我的后端数据是在java中。所以请有人告诉我如何为我的angularjs应用程序制作一个web代理。

或者无论如何都要绕过我浏览器的cors请求。

3 个答案:

答案 0 :(得分:1)

用户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

答案 1 :(得分:1)

快速解决使用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
}

希望这会有所帮助。

答案 2 :(得分:0)

如下所示解码你的数组..

$newarr= json_decode('urjsonstring');

extract($newarr);

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