I tried to access an id from a URL "myfolder/mycontroller/mymethode/123" which I call into an AJAXcall.
I cannot access them, in "mycontroller" under the "mymethode". I tried to output $_GET, $_POST, $this->input->get(), $this->input->post(), but all array are empty.
In the Controller/mycontroller I have this
public function mymethode($listid=false)
{
echo "listid: $listid";
print_r($_GET);
print_r($_POST);
print_r($this->input->get());
print_r($this->input->post());
}
The Ajax call is this and is ok with Status 200.
$.ajax({
url: http://mydomein.com/myfolder/mycontroller/mymethode/123,
type: "POST",
method: 'post',
data: form + "&" + additional_data + csrfName + "=" + csrfHash,
dataType: 'json',
cache: false,
success: function(res){...
If I tried to open the URL directly, I have the same problem.
What can the reason for it be?
Use this (if this is not HMVC)
in route.php
# You may need first route since you're accepting null on method
$route['mymethode] = 'mycontroller/mymethode';
$route['mymethode/(:num)'] = 'mycontroller/mymethode/$1';
In AJAX
url: http://mydomein.com/mymethode/123,
Make sure your sites run without index.php
In controller
public function mymethode($listid=null)
Related
I'm trying to use the google chart api in an XPages application.
I'm using the code example given by the documentation : https://developers.google.com/chart/interactive/docs/php_example#exampleusingphphtml-file
I have to replace the call to the php page by a call to an LS agent.
var jsonData = $.ajax({
url: "getData.php",
dataType: "json",
async: false
}).responseText;
So my code goes to :
var jsonData = $.ajax({
url: "http://server/database/agent?openagent",
dataType: "json",
async: false
}).responseText;
On my local domino server, it works fine.
On the production domino server, I get nothing. The chart is not drawn. After debugging the js client side, it seems the ajax call is expecting an authentification even if I had to log in before.
The anonymous access is not allowed on both servers.
The security level seems to be same on both environments
Any help will be welcome (or any other way to proceed if I'm wrong).
Thank you
If you are able to draw the google chart in your local server, but not in production server, this means it is your server issue.
You can add authentication header in your jquery ajax call to make authenticated ajax request
$.ajax({
headers: {
"Authorization": "Bearer <TOKEN HERE>"
}
})
You can also send username and password in jquery ajax call, to make authenticated request. Here is the sample code from the link
$.ajax({
type: 'GET',
url: 'url',
dataType: 'json',
//whatever you need
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', make_base_auth(user, password));
},
success: function () {});
});
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return 'Basic ' + hash;
}
at the end, I tried to run the ajax request through dojo instead of Jquery.
My codes became this one :
var jsonData = dojo.xhrGet({
url: "http://server/database/agent?openagent",
handleAs:"json",
...
})
I did no changes at the security level or anything else.
I do not understand why the jquery syntax is not working as well the dojo syntax.
anyway, it is working now.
Many thanks to all for your suggestions
Unable to create Delete action in Laravel.
I am getting Not Found or Token mismatch errors all the time.
My controller:
class TranslationController extends Controller
{
public function destroy($id)
{
//$id = 1;
/*$translation = Translation::find($id);
$translation->delete();*/
}
....
}
Ajax call:
/* Delete given translation */
var url = "translation";
var id = 1;
$.ajax({
method: 'DELETE',
url: url + '/' + id,
// data: {'id': id, '_token': token},
success: function() {
}
});
This would give: TokenMismatchException in VerifyCsrfToken.php line 53:
If I try:
url: url + '/' + id,
data: {'_token': token}, // token is equal to csrf_token
I have: NotFoundHttpException in Controller.php line 269:
Routes:
Route::controller('translation', 'TranslationController');
Otherwise I am using Laravel 5 default Middleware, I have not changed anything related to csrf.
NotFoundHttpException means that either the route for the particular request with the particular HTTP verb has not been specified, or the action (i.e. the controller method) that is mapped to the verb for the route is wrongly implemented.
Since you've mentioned in the post that the TranslationController is defined as an implicit controller,
Route::controller('translation', 'TranslationController');
and from the controller code you've posted, it's quite obvious that you have not defined the verb for the destroy method in your controller TranslationController.
If you do a php artisan route:list in your projects root directory with a terminal/command line interface, you'll see the listing of the registered HTTP verbs, mapping to the corresponding URIs, and the actions.
To define a particular method in an implicit controller, the verb (GET, PUT, POST, DELETE) should precede the actual function name.
Make sure that the destroy method looks like the following in your controller:
public function deleteDestroy($id){
//delete logic for the resource
}
Note:
Laravel by default requires that the csrf token is passed along with a particular RESTful request, so do not remove data: {'_token': token} from your AJAX call.
Update
Forgot to mention that the url in your AJAX call should also be changed to the following in order to work, because this is how Laravel's implicit controllers define the route for a DELETE request:
var url = "translation/destroy";
Here is documentation about method spoofing. You need to send a POST ajax request with _method field set to DELETE
$.ajax({
method: 'POST',
url: url + '/' + id,
data: {
'id': id,
'_token': token,
'_method' : 'DELETE'
},
success: function() {
}
});
You could try defining your route that way
Route::delete('translation/{id}',array('uses' => 'TranslationController#destroy'));
In this case your AJAX won't change. But if you want keep this Route
Route::controller('translation', 'TranslationController');
You must change your Ajax request to:
/* Delete given translation */
var url = "translation/destroy"; // You must specify the action
var id = 1;
$.ajax({
method: 'DELETE',
url: url + '/' + id,
data: {'_token': token},
success: function() {
}
});
You should to send token via header. ( especially in 5.2 version )
$.ajax({
type: "post",
url: "/routeurl",
headers: { 'X-CSRF-Token': "{!! csrf_field() !!}" },
success: function(msg){
// msg
}
});
I want to perform delete operation with ajax in magento admin panel. I have created my own function deleteAttrGroupKey() and put it to controller GrouppricebackendController.php. I am trying to access deleteAttrGroupKey() function from my template(phtml) file but getting invalid path. code is
jQuery(".deleteAttrKeyId").on("click",function(){
var baseUrl="<?php echo Mage::getBaseUrl();?>";
var idArr=this.id.split("-");
attrKeyId=idArr[1];
alert(this.id);
jQuery.ajax({
type: "POST",
dataType: "JSON",
data :{'id':attrKeyId},
url :baseUrl+"adminhtml/grouppricebackend/deleteAttrGroupKey",
complete:function(){alert("completed");
},
success:function(event){
alert("deleted"+event);
}
});
});
how can I pass correct url for this ?
Do I need to add deleteAttrGroupKey() function in config.xml ?
You should ajax request like this
new Ajax.Request("<?php echo $this->getUrl('adminhtml/grouppricebackend/deleteAttrGroupKey') ?>", {
method: 'Post',
parameters: {"id":attrKeyId},
onComplete: function(transport) {
alert(transport.responseText);
}
});
Yes you need to include deleteAttrGroupKey function in config.xml file
I have URL like this http://localhost/site/section/80#10
and the function section like this
function section(id = null){
echo id;
}
The question is how can i get #10 from URL ?
You can't. The hash portion of the URL isn't actually sent to the server.
You can get the hash using javascript's window.location.hash and send it to the server with ajax.
A jQuery example:
$.ajax({
type: 'POST',
url: 'http://site.com/controller/method_that_does_something_with_hash',
data: {
hash: window.location.hash
},
success: function(response){
// do something here with whatever the server responded with
}
});
I want to send a string as an ajax Post parameter.
The following code:
$.ajax({
type: "POST",
url: "http://nakolesah.ru/",
data: 'foo=bar&ca$libri=no$libri',
success: function(msg){
alert('wow'+msg);
}
});
Is not working. Why?
Try like this:
$.ajax({
type: 'POST',
// make sure you respect the same origin policy with this url:
// http://en.wikipedia.org/wiki/Same_origin_policy
url: 'http://nakolesah.ru/',
data: {
'foo': 'bar',
'ca$libri': 'no$libri' // <-- the $ sign in the parameter name seems unusual, I would avoid it
},
success: function(msg){
alert('wow' + msg);
}
});
$.ajax({
type: 'POST',
url:'http://nakolesah.ru/',
data:'foo='+ bar+'&calibri='+ nolibri,
success: function(msg){
alert('wow' + msg);
}
});
I see that they did not understand your question.
Answer is: add "traditional" parameter to your ajax call like this:
$.ajax({
traditional: true,
type: "POST",
url: url,
data: custom,
success: ok,
dataType: "json"
});
And it will work with parameters PASSED AS A STRING.
For a similar application I had to wrap my data object with JSON.stringify() like this:
data: JSON.stringify({
'foo': 'bar',
'ca$libri': 'no$libri'
}),
The API was working with a REST client but couldn't get it to function with jquery ajax in the browser. stringify was the solution.
Not sure whether this is still actual.. just for future readers.
If what you really want is to pass your parameters as part of the URL, you should probably use jQuery.param().
Not a direct answer to your question.. But following is the only syntax that used to work for me -
data: '{"winNumber": "' + win + '"}',
And the parameter-name match with the argument of the server method
I was facing the problem in passing string value to string parameters in Ajax. After so much googling, i have come up with a custom solution as below.
var bar = 'xyz';
var calibri = 'no$libri';
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "http://nakolesah.ru/",
data: '{ foo: \'' + bar + '\', zoo: \'' + calibri + '\'}',
success: function(msg){
alert('wow'+msg);
},
});
Here, bar and calibri are two string variables and you can pass whatever string value to respective string parameters in web method.
I have also faced this exact problem. But I have got a solution and it worked perfectly. I have needed to pass the parameters which are already produced by javascript function. So below code is working for me. I used ColdFusion for the backend. I just directly used the parameters as a variable.
$.ajax({
url: "https://myexampleurl.com/myactionfile.cfm",
type: "POST",
data : {paramert1: variable1,parameter2: variable2},
success: function(data){
console.log(data);
} )};
Instead of this, encode the POST request as a string and pass to the data parameter,
var requestData = "Param1=" + encodeURIComponent(jsParam1) + "&Param2="+ encodeURIComponent(jsParam2);
var request = $.ajax({
url: page + "?" + getVars,
method: "POST",
data: requestData,
dataType: "html",
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
});