I have 2 (local) subdomains: kohana.local.com and wordpress.local.com. jQuery plugin (.js) is located on kohana.local.com domain and takes care of rating articles and retrieving rate count on both domains. Controller_Rating extends Controller. Method (action_getrating) has following code (kohana 3.2):
if($this->request->post() && $this->request->is_ajax()){
$this->auto_render = FALSE;
echo "{$_REQUEST['callback']}(".json_encode($data).")";
}
Ajax call:
$.ajax({
type: "POST",
url: "http://kohana.local.com/rating/getrating",
dataType: "jsonp",
data: { some_id: id },
success: function(json){
//do something
}
});
When ajax call gets issued from kohana.local.com, everything works great. If it's issued from wordpress.local.com $this->request->is_ajax() is false, and method is not "post", but "get" somehow. What is the reason for this, and how to make it work? Post is required and is_ajax is good for security and validation.
Thanks in advance.
EDIT:
post to jsonp is not possible, so i can't use this approach. i'll have to try to find the solution in the direction of json
You can simply use json dataType, so you don't need to use callbacks. Just add header Access-Control-Allow-Origin to server which requests are made (kohana.local.com).
All domains are allowed:
Access-Control-Allow-Origin: *
Or specify allowed domain:
Access-Control-Allow-Origin: http://wordpress.local.host
Spec: http://www.w3.org/TR/2008/WD-access-control-20080912/#access-control-allow-origin
Multiple domain solution: Access-Control-Allow-Origin Multiple Origin Domains?
Related
I am trying to delete a post when clicking the (x) icon using Ajax request
here is my html code relating to this part :
I copied the JS and php parts from google , I've never written an Ajax request before
so I cannot figure out where I did wrong, please help
It seems that you have a problem with the right location of the php file.
It seems to be located on /static/delete.php, but you are calling /post/delete.php.
Other than that, you are not (at the moment) using DELETE, but POST.
Some frameworks use the POST as the method, but require some extra field (like _method on Laravel) to be present on the form, as some (if not all) HTTP servers do not accept methods other than GET or POST
$('#btnDeleteEmployees').click(function () {
$.ajax({
url: '/api/employees' + empID,
method: 'DELETE',
headers: {
"Content-Type": "application/json"
},
success: function (res) {
},
error: function (res) {
}
});
});
I'm trying to post newsletter form data to a php page with ajax but when I echo $_SERVER['REQUEST_METHOD'] on the php page it prints GET(with no variables). I have used similar code on multiple websites with no problems. (jquery v3.2.1)
$.ajax({
method: 'post',
dataType: 'json',
url: '<?=$site_url?>/templates/xxxx/contact_send/email_send.php',
data: JSON.stringify(dataString),
contentType: 'application/JSON;charset=UTF-8',
success: function () {
console.log('success');
}
});
Network log
echo $_SERVER['REQUEST_METHOD']
Response
browser of method is GET....(second image)
you should see network log image the response
I would try to narrow down the problem by making a new HTML file with just your javascript code in it sending some fake data and a new PHP file with just echo $SERVER['REQUEST']; If it works you have some extra code that's messing it up. Otherwise it's some server issue. Did you make sure that you don't have .htaccess to forbid POST? Try adding this to the .htaccess Access-Control-Allow-Methods: POST, GET, OPTIONS
I'm having a bizarre issue where my POST request is being treated as a GET - this only happens on the LIVE environment and works fine locally. I have the correct POST route set up in laravel.
Would there be a case where jQuery would default to GET on a server environment - I'm currently accessing the site via an IP rather than the domain while the DNS resolves, could this perhaps cause an issue?
Route::post('/ajax/sale/filter', 'SalesController#ajaxFilterOptions');
$.ajax({ url: '/ajax/sale/filter/',
data: {filter: options, sale_id: window.saleId, outlet_type: outletType},
type: 'POST',
cache: false,
dataType: 'JSON',
success: _.bind(function (data) {
console.log(data)
}, this)
});
Your code lines up fine, you may want to check your htaccess file to see if a rewrite is happening (or corresponding rewrite with another httpd), or switch to $.post instead of $.ajax to make sure/force it as a POST request.
I know there is an accepted answer here. But whoever comes this page might be interested in knowing that if you remove the leading slash this will work fine. So in the example given, use url "/ajax/sale/filter" instead of "/ajax/sale/filter/"
I am having a problem that is really stumping me. In my Opencart installation my account registration page isn't working. It is a multisite and the page works fine on the other site. The problem is with the zones, when it tries to get zones based on the country it throws an error. When I examined it with Firebug I see that it sends an OPTIONS request instead of a GET request like it does when generating a successful request on the other page. Unfortunately this isn't the only problem, I was able to get it to send a GET request by specifying "crossDomain: 'false'" as an argument in the .ajax call and that still doesn't fix the error. The cookies being sent and returned are also different. I'm trying to find the underlying problem or at least something that will fix the issue.
Edit:
I added responded to questions in a comment as it Stackoverflow did allow me any more links.
2nd Edit:
I've found that the way you access the registration page matters. Some links to it don't generate any problems. I'm thinking more and more that this probably has something to do with cookies.
The OPTIONS request is indeed very strange in this case and almost looks like the client side (JS/browser) do not know what request methods could be used on the server.
Anyway, in the template catalog/view/theme/<YOUR_THEME>/template/account/register.tpl find this piece of code (almost end of file):
<script type="text/javascript">
$('select[name=\'country_id\']').bind('change', function() {
$.ajax({
url: 'index.php?route=account/register/country&country_id=' + this.value,
dataType: 'json',
beforeSend: function() {
and before dataType: 'json', add type: 'get', or type: 'post', so You should end up with:
<script type="text/javascript">
$('select[name=\'country_id\']').bind('change', function() {
$.ajax({
url: 'index.php?route=account/register/country&country_id=' + this.value,
type: 'post',
dataType: 'json',
beforeSend: function() {
By this You specify which concrete HTTP request method should be used.
When I'm trying to make an AJAX call from php (localhost) to django (localhost:8000), it throws the following error.
XMLHttpRequest cannot load
http://localhost:8000/project/login/uid=bimt;token=KAMWMS151UWP67Q.
Origin http://localhost is not allowed by Access-Control-Allow-Origin.
$(document).on('click', '.login', function(event) {
var username = $('#username').val();
var token = $('#token').val();
$.ajax({
type: "POST",
url: "http://localhost:8000/project/login/uid=" + username + ";token=" + token,
success: function (html) {
alert(html);
}
});
});
Because the port is not the same, it's considered a cross origin request.
You must set a Access-Control-Allow-Origin header in the script you are requesting.
Learn more:
http://enable-cors.org/server.html
or, specifically for django:
http://chase-seibert.github.io/blog/2012/01/27/using-access-control-allow-origin-to-make-cross-domain-post-requests-from-javsacript.html
1) php does not make ajax requests. php executes on the server side; javascript executes on the client side, and it is js that makes ajax requests to the server.
2) js does not allow you to make ajax requests to a different host than the one from which the current page was obtained from.
AJAX, by default, can't make cross-domain requests.
You should refer to this link.
Try, something like:
$('#yourClickButtonId').click(function(){
$.ajax({
type: 'POST',
url: 'project/login?uid='+$('#usename')+'&token='+$('#token'),
success: function(data){
alert(data);
}
});
});
You should just use a relative path.
I swapped my internal IP (192.168.1.x) in the place of localhost and was able to call a XMLHttpRequest on it. This should do the trick without messing with cross origin policy.