douwei2966 2016-06-12 15:17
浏览 130

标题CORS“Access-Control-Allow-Origin”缺少Codeigniter

I'm trying to learn how to use Ajax in Codeigniter . This is why I tried to make a small view and a test controller but does not seem to work. Can you help me? This is my view:

    <script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
<form name="modulo">
    <p>Nome</p>
    <p><input type="text" name="nome" id="nome"=></p>
    <p>Cognome</p>
    <p><input type="text" name="cognome" id="cognome"></p>
    <input type="button" id="bottone" value="Invia i dati">
</form>
<div id="risultato"></div>
<script type="text/javascript">
$(document).ready(function() {
  $("#bottone").click(function(){
    var nome = $("#nome").val();
    var cognome = $("#cognome").val();
    $.ajax({
      type: "POST",
      url: "http://lifedesktop/welcome/ajax",
      data: "nome=" + nome + "&cognome=" + cognome,
            crossOrigin: true,
      dataType: "html",
      success: function(msg)
      {
        $("#risultato").html(msg);
      },
      error: function()
      {
        alert("Don't work...");
      }
    });
  });
});
</script>

And this is my controller:

public function ajax()
    {
        if($this->input->is_ajax_request()){
        $nome = $_POST["nome"];
        $cognome = $_POST["cognome"];
        if ($nome == "" || $cognome == "")
        {
                echo "Inserire nome e cognome!";
        }
        else
        {
                echo $nome . " " . $cognome;
        }
        }
    }

In the console web i have this error:

header CORS “Access-Control-Allow-Origin” missing.

Can you help me?

  • 写回答

1条回答 默认 最新

  • douxing9567 2016-06-12 18:06
    关注

    If you have access to the api, so set this header to your php code:

    header('Access-Control-Allow-Origin: *');  
    

    If not, you can only use that api in your own web page, by installing some CORS plugins for chrome or firefox like this. (It's not possible to share this with users)

    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题