【问题标题】:web-proxy for angularjs applicationangularjs 应用程序的网络代理
【发布时间】:2016-04-01 10:48:50
【问题描述】:

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

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

【问题讨论】:

    标签: angularjs webproxy


    【解决方案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
    }
    

    希望这会有所帮助。

    【讨论】:

    • 给出这个输出警告:在 C:\xampp\htdocs\so\test.php 第 20 行数组 () 中为 foreach() 提供的参数无效注意:未定义的偏移量:C:\xampp 中的 0 \htdocs\so\test.php 第 25 行
    • 你能告诉我 $file 是我的 json 数据,它来自 post 方法吗?
    • 是的。 $file 是你的数据。
    • Pathik,我不知道那是什么东西
    • 好的,让我试试这些家伙,你能检查一下我在我的问题中发布的整个 json 对象吗,Mawia
    【解决方案2】:

    用户 json_decodetrue 参数

    $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
    

    【讨论】:

    • 如果我有n个studentid怎么办。我怎样才能写出 $data。
    • 好的,任何与此数据有关的示例,因为我是这一行的新手。我的表单包含全部数据,当我从我的 html 页面单击提交时,这些数据必须转到 mysql 数据库。请举个例子
    • @RamansathiyaNarayanan 给我样本数据你得到的响应
    • @RamansathiyaNarayanan 你能看看我更新的答案吗
    • 好的,那么我怎样才能输入我的代码而不是像 $_POST 之类的 echo
    【解决方案3】:

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

    $newarr= json_decode('urjsonstring');
    
    extract($newarr);
    
    $query="insert into stud values($studentid, $firstname,$lastname...)";
    

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 2016-07-05
      • 2011-04-16
      • 2010-11-15
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-02
      相关资源
      最近更新 更多