Ionic - $ http.get()不发送授权标头

时间:2015-11-03 22:54:57

标签: angularjs http-headers ionic-framework ionic

Ionic App不发送授权标头。尽管授权标头列在允许的标头中,但标头值根本不存在。 我使用NodeJS Express和MongoDB作为后端这里是我的CORS标题

private void DeleteFamBtn_Click(object sender, EventArgs e)
    {
        List<DataGridViewRow> selectedRows = (from row in DataGridViewFamille.Rows.Cast<DataGridViewRow>()
                                              where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true
                                              select row).ToList();


        if (MessageBox.Show(string.Format("Do you want to delete {0} rows?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            //delete Nature de Charge before delete Famille
            foreach (DataGridViewRow row in selectedRows)
            {
                try
                {
                    using (SqlConnection con = new SqlConnection(connstring))
                    {
                        con.Open();
                        using (SqlCommand command = new SqlCommand("DELETE NatureCharge FROM NatureCharge n INNER JOIN Famille f on n.IdFam = f.IdFam WHERE IdNat = @IdNat", con))
                        {
                            command.ExecuteNonQuery();
                        }
                        con.Close();
                    }
                }
                catch (SystemException ex)
                {
                    MessageBox.Show(string.Format("An error occurred: {0}", ex.Message));
                }
            }

            //Delete Famille
            foreach (DataGridViewRow row in selectedRows)
            {
                using (SqlConnection con = new SqlConnection(connstring))
                {
                    using (SqlCommand cmd = new SqlCommand("DELETE FROM Famille WHERE IdFam = @IdFam", con))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("@IdFam", row.Cells["IdFam"].Value);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }

2 个答案:

答案 0 :(得分:1)

您可以在进行$ http调用之前传递auth标头。我已经用这种方式了,这对我来说是API身份验证,

$http.defaults.headers.get = { 'x-username' User.email,
                           'x-token':authToken.getToken(), 
                           'Content-Type':'application/json'};

$http.get(URL+'/api).then(function(res){console.log('success')});

希望这对你有所帮助。

答案 1 :(得分:0)

我有同样的问题。我想出解决问题的唯一方法是添加:

"proxies": [
{
  "path": "/<your local url>",
  "proxyUrl": "http://<remote_url>"
}
],

到我的ionic.project文件。