Reputation: 1
I'm a long time overflower and eventually signed up. I've gone through tons of threads but none could help me with my problem. I am about to create a RESTful API with the slim framework in php and have problems with the crossdomain put requests.
Let me give you a quick introduction to what I've done. After wrtining the first GET requests I was looking for a method to test my API easily and found the "RESTClient" Firefox addon, it worked well so I continued using it. After a few tests I tested a self-written code on another server and there the problem occured.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at %1$S. (Reason: CORS preflight channel did not succeed).
What I wonder the most about is the fact that the Firefox addon had no problem to make the request at any time, it always showed the expected response. The error above just occured when I tried to send a request by my own code. I read a lot about cross-origin requests but nothing helped me, I hope you can.
Now first of all let me show you my slim framework php code
<?php
header('access-control-allow-origin: *');
header('Content-Type: application/json');
function output($arr){
echo json_encode($arr);
exit;
}
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->put(
'/put',
function () {
$output = array("status"=>"200","message"=>"This is a put test");
output($output);
}
);
$app->run();
?>
And my test.html
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script type="text/javascript">
(function($) {
var url = 'URL';
$.ajax({
type: 'put',
url: url,
async: false,
crossDomain:true,
dataType: 'json',
success: function(json) {
console.log(json);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
</script>
</body>
</html>
Can someone please explain to me how I can get this PUT request working. I am really in this despair mood right now. I understand all that cross-domain topic but why is he not letting it go after i set the access-control-allow-origin in the php code ? What is the firefox addon doing different? ...
Upvotes: 0
Views: 1349