1

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.

0

3 Answers 3

1

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] => [email protected]
            [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.

Sign up to request clarification or add additional context in comments.

17 Comments

giving this output Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\so\test.php on line 20 Array ( ) Notice: Undefined offset: 0 in C:\xampp\htdocs\so\test.php on line 25
can you tell me $file is my json data which comes from post method.
Yes. $file is your data.
Pathik, I got no idea what is that thing
okay, let me try these guys, can you please check my whole json object which I posted in my question,Mawia
|
1

User json_decode with 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'];

EDIT: For multiple json data:

$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>";
}

Output:

StudentID: 1
StudentID: 3
StudentID: 5

6 Comments

what if I have got n number of studentid. How can I write that $data.
Okay, any example with this data because Im new to this line. My form contains totally datas and when I click submit from my html page, these datas has to go to mysql database. Any example please
@RamansathiyaNarayanan give me sample data what you get in resopnse
@RamansathiyaNarayanan can you please cehck my updated answer
okay, so how can I put for my code instead of echo like $_POST or something
|
0

Decode ur array like below..

$newarr= json_decode('urjsonstring');

extract($newarr);

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.