Reputation: 986
In console log getting the bellow message Reason: CORS header ‘Access-Control-Allow-Origin’ missing So I checked few stack overflow solution but not able to resolve some one can help me to resolve it.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function ()
{
var save_temp = {
"token": "e2c420d928d4bf8ce0ff2ec19b371514"
};
var comment_data = JSON.stringify(save_temp);
//alert(comment_data);
var request = $.ajax({
url: "http://vyhub.com/irmtapi/dailyreport/checkReport",
type: "POST",
data:comment_data,
contentType: "application/json; charset=utf-8",
dataType: 'json',
});
request.done(function(msg)
{
// $("#log").html( msg );
alert(msg)
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
});
</script>
Upvotes: 1
Views: 373
Reputation: 167240
This is clearly a problem with the server side code, which doesn't send the right headers to allow the Cross Origin Request Sharing. You need to enable CORS through two ways in Apache Server with PHP.
Method 1: Using Apache Server's settings.
Using Apache's .htaccess
, you can enable the CORS by adding the following line:
Header set Access-Control-Allow-Origin "*"
For this to work, you need to make sure that the mod_headers
module is enabled. You can enable this by using sudo a2enmod headers
. You need sudo permissions for this. Also, you need to restart Apache Server after changing the configuration by using sudo service apache2 reload
.
Method 2: Using PHP Code.
Alternatively, if you don't have much control over the server management, you can enable it in your script that responds to your request. Just add this at the top of the PHP file, so that the browser is allowed AJAX Access:
<?php
header("Access-Control-Allow-Origin: *");
Let me know if you have any further questions. For more information, please find the references below:
Upvotes: 1