1

I have this code in javascript,

  $(document).on('click', '.event-attendance-official', function() {
    var id = $(this).parents('tr').data('event');
    var data = {
      id: id
    }

    submit(data, route('attendance.official'), function(data) {
      console.log(data);
    });
  });

and the submit function

  function submit(data, url, callback) {
    $.ajax({
      type: method,
      url: url,
      data: data,
      dataType: 'json',
      beforeSend: function(request) {
        request.setRequestHeader("X-CSRF-TOKEN", $('meta[name="csrf-token"]').attr('content'));
      },
      complete: function(data) {
      },
      success: function(data) {
        callback(data);
      },
      error: function(data) {
        console.log('Error:');
      }
    });
  }

which submit an id to this code

public function getOfficialAttendance(Request $event)
{
    return Attendance::with('user')
     ->where('event_id', '=', $event->id)
     ->where('did_attend', '=', 'true')
     ->get();        
}

and the given route

Route::prefix('attendance')->group(function() {
  Route::name('attendance.official')->post('/get/official/attendance','AttendanceController@getOfficialAttendance');
});

What this code does, is when I click on .event-attendance-official a modal should prompt with the result from the request by the given ID using post.

the problem, is that, whenever, click on the same class I got this error in the console log.

Failed to load http://localhost/attendance/get/official/attendance: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://test.dev' is therefore not allowed access.

have you encountered the same problem? what did you do to resolved?

Rob
  • 14,746
  • 28
  • 47
  • 65
Liz
  • 23
  • 3
  • Possible duplicate of [No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '...' is therefore not allowed access](https://stackoverflow.com/questions/20433655/no-access-control-allow-origin-header-is-present-on-the-requested-resource-or) – Rob Oct 12 '17 at 02:29

1 Answers1

0

This is a CORS case (Cross-origin resource sharing), which by default don't allow web pages from one site(URL) access others sites' ressources.

add this line before return should be ok.

header("Access-Control-Allow-Origin: *");
jetwaves
  • 51
  • 3