behnampmdg3's avatar

Cross-Origin Request Blocked, in Firefox.

Cross-Origin Request Blocked, in Firefox. On Chrome it's good.

===================

Hi;

I use ajax to update db.

It sends the form data and updates the DB well, however, the response doesn't work. Inspector says:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://webmoosh.com/plc_template_update. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). It's open you can see it live ===> here <===

What am I doing wrong?

Thanks

function submit_form(form_id, form_box)
    {
        $("#"+form_id).submit(function(e) 
            {
                var url = '<?php echo base_url('plc_template_update');?>'; 
                $.ajax({
                   type: "POST",
                   url: url,
                   data: $("#"+form_id).serialize() + '&item_type=' + '<?php echo $item_type;?>' + '&item_id=' + '<?php echo $item_id;?>',
                   success: function(data)
                   {
                      $("#"+form_box).addClass('hide-me');
                      $("#success").removeClass('hide-me');
                      window.setTimeout(function(){
                        $("#success").addClass('hide-me');
                    }, 1000);
                   }
                 });
            e.preventDefault(); 
        });
    }
public function index()
        {
            if(!isset($_POST['item_id']) || !ctype_digit($_POST['item_id']))
                {
                    exit();
                }
            if($_POST['item_type']=='plc')
                {
                    //Uncomment when tests are over
                    // if(!$this->plc_library->is_this_plc_owner($_POST['item_id']))
                    //  {
                    //      exit();
                    //  }
                }       
            $data = array();
            foreach($_POST as $val=>$row)
                {
                    if($val!='item_id' && $val!='item_type' )
                        {
                            $data[$val]=$row;
                        }   
                }
            $this->db->where('id', $_POST['id']);
            $this->db->update('launch_page_elements', $data);
            http_response_code(200);
        }
0 likes
2 replies
lostdreamer_nl's avatar

Before you submit the form:

  • press F12 (Developer Toolbar)
  • Go to the Network tab
  • Submit your form
  • click on the new URL being shown of this POST request, on the right panel you will see some info about this request, check out the response headers to see if the CORS header is actually there.

If so, could you make a screenshot of that and post it here so we can see the headers? There might be something wrong there (dont forget to strip out your domain).

behnampmdg3's avatar
behnampmdg3
OP
Best Answer
Level 5

Found it.

I was on the page with http white the form was going to https.

Changing to https fixed it.

Thanks

Please or to participate in this conversation.