Angular AJAX发布问题

时间:2015-08-18 14:38:39

标签: php angularjs ionic-framework ionic

我正在开发一个离子应用程序,需要将用户提交的信息发布到数据库。我正在与为此表单提交创建单独的php脚本的人合作,但是每次我尝试从应用程序发布时,我在使用json afeld时不使用代理或404错误时会出现cors问题...

PHP看起来像这样:

<?php

$data = json_decode(file_get_contents("php://input"));
$celeb = $data->celeb;
$camp = $data->camp;
$spirit = $data->spirit;

$sport = $data->sport;
$bizs = $data->bizs;
$entrep = $data->entrep;

$young = $data->young;
$conser = $data->conser;
$saty = $data->saty;

$name = $data->name;
$surname = $data->surname;
$email = $data->email;

$contacts = $data->contacts;

$con = mysql_connect('localhost', 'yarp', 'arpi');
mysql_select_db('yeah', $con);

$qry_em = 'select count(*) as cnt from users where email ="' . $email . '"';
$qry_res = mysql_query($qry_em);
$res = mysql_fetch_assoc($qry_res);

if ($res['cnt'] == 0) {
    $qry = 'INSERT INTO test (celeb,camp,spirit,sport,bizs,entrep,young,conser,saty,name,surname,email,contacts) values ("' . $celeb . '","' . $camp . '",' .$spirit . ','.$sport. ','.$bizs. ','.$entrep. ','.$young. ','.$conser. ','.$saty. ','.$name. ','.$surname. ','.$email. ','.$contacts. ')';
    $qry_res = mysql_query($qry);
    if ($qry_res) {
        $arr = array('msg' => "Submitted Successfully!!!", 'error' => '');
        $jsn = json_encode($arr);
        print_r($jsn);
    } else {
        $arr = array('msg' => "", 'error' => 'Error In Submit');
        $jsn = json_encode($arr);
        print_r($jsn);
    }
} else {
    $arr = array('msg' => "", 'error' => 'A submit has already been cast using this email');
    $jsn = json_encode($arr);
    print_r($jsn);
}


?>

我的控制器看起来像这样:

.controller('FrmController', function ($scope , $http) {
                $scope.errors = [];
                $scope.msgs = [];

                $scope.vote = function() {

                    $scope.errors.splice(0, $scope.errors.length);
                    $scope.msgs.splice(0, $scope.msgs.length);

                    $http.post('https://jsonp.afeld.me/?url=http://www.examplesite.com/submit.php', {

                        'celeb'     : $scope.celeb, 
                        'camp'      : $scope.camp, 
                        'spirit'    : $scope.spirit,
                        'sport'     : $scope.sport,
                        'bizs'      : $scope.bizs, 
                        'entrep'    : $scope.entrep, 
                        'young'     : $scope.young,
                        'conser'    : $scope.conser,
                        'saty'      : $scope.saty, 
                        'name'      : $scope.name, 
                        'surname'   : $scope.surname,
                        'email'     : $scope.email,
                        'contacts'  : $scope.contacts

                    }
                    ).success(function(data, status, headers, config) {
                        if (data.msg != '')
                        {

                          console.log(data.msg);
                            $scope.msgs.push(data.msg);
                        }
                        else
                        {
                          console.log(data.error);
                            $scope.errors.push(data.error);
                        }
                    }).error(function(data, status) { 
                        $scope.errors.push(status);
                    });
                }

        })

submit.php文件肯定在地址,所以我不太确定我哪里错了...

2 个答案:

答案 0 :(得分:1)

所以我对php知之甚少,但我很确定你需要在php文件中启用cors:

<?php
 header("Access-Control-Allow-Origin: *");

注意:与PHP标头功能的所有用法一样,这必须在从服务器发送任何输出之前。

答案 1 :(得分:1)

正如Jess Paton所说,你应该使用它。但是,一个更好的例子(来自我的博客文章,下面链接)是这样的:

<?php
 //http://stackoverflow.com/questions/18382740/cors-not-working-php
 if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }


    //http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined
    $postdata = file_get_contents("php://input");
 if (isset($postdata)) {
 $request = json_decode($postdata);
 $username = $request->username;

 if ($username != "") {
 echo "Server returns: " . $username;
 }
 else {
 echo "Empty username parameter!";
 }
 }
 else {
 echo "Not called properly with username parameter!";
 }
?>

有关详细信息,如果您有兴趣,我会编写一个分步教程,介绍如何将数据从Ionic app发布到PHP服务器,以及Github example,您可以在此处查看: http://www.nikola-breznjak.com/blog/codeproject/posting-data-from-ionic-app-to-php-server/