angularjswebproxy

web-proxy for angularjs application


I have to create a web-proxy script for my angularjs files because I got the error of CORS(Cross Origin Request Method) and I dont have any options to use Access Control Allow Origin because I cant make any changes to my server end. My backend data is in java. So please someone tell me how to make a web-proxy for my angularjs application.

Or is there anyway to bypass the cors request from my browser.


Solution

  • A quick work around using foreach and json_decode.

    If your print_r($json) comes in this format:

    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
            )
    
    )
    

    This will do the trick:

     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
    }
    

    Hope this will help.