Opencart - Way to call ajax request in Admin Panel - php

I want to fetch some data using ajax request in Opencart Admin panel. I have simply created a controller which only returns data in json format. Using below code:
$.ajax({
type: 'post',
url: 'index.php?route=common/ajaxdata/functionName&token=<?php echo $token; ?>',
data: data,
dataType: 'json',
success: function(json) {
if (json['success']) {
alert(json['success']);
}
}
});
Can anybody please let know whether this is correct or any other built in functionality available in Opencart for ajax requests? So that I can proceed and implement this in my rest of the pages.
Thanks

$.ajax({
type: "POST",
url: "http://your url write hare",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
"propertytypename": property,
"propertytypecreatedby": property_name
}),
success: alert("success"),
error: alert("Fail")
});

Related

Sending variable with ajax

I'm having some troubles trying to send an id through ajax script,
I'm trying to create an address book, when you select a contact it load all his information in another page and the ajax script load this page inside the main. Everything is working except to send the ID of the contact. There's a bit of the code :
$.ajax({
url: "select.php",
dataType: "html",
data: {id: id},
success: function(Result) {
$('#result').html(Result);
}
});
PHP
$id = $_POST['id'];
echo $id;
$sql = "SELECT * FROM ca11 WHERE contact_id= $id";
does anyone have an idea ?
you need to add which type of request you are making, as your question, you can call post request as below
$.ajax({
type: "POST",
url: "select.php",
dataType: "html",
data: {id: id},
success: function(Result) {
$('#result').html(Result);
}
});
For more information you can refer this link http://api.jquery.com/jQuery.post/

Pass multiple parameters through GET method in URL of ajax call

I want to pass multiple parameters through GET method in URL of ajax call but its not working.any kind help would be appreciated Thankyou
$.ajax({
type: "GET",
contentType: "text/xml",
dataType: 'json',
async:true,
cache: false,
headers:{ 'Access-Control-Allow-Origin': '*'},
iurl: "http://maps.googleapis.com/maps/api/distancematrix/xml?origins="+dest_lat+"&"+dest_lon+"|"+data[i][3]+"&"+data[i][4],
success: function(msg) {
alert(msg);
}
});

$.ajax and method post not work correctly

I have a simple code-form like this:
$.ajax({
url: "http://myurl/test.php",
type: "POST",
dataType: "html",
data: {
action: "login"
},
success: function (data) {
console.log(data)
}
});
When on PHP server page, I try to print $_POST["action"] yet this field is blank. Why does it work fine if I use $.post(url,data,function)?
$.ajax({
url: "http://myurl/test.php",
type: "POST",
dataType: "html",
data: {
method: "login"
},
success: function (data) {
console.log(data);
}
});
login is a php function
You may use method instead of type.
$.ajax({
url: "http://myurl/test.php",
method: "POST", // <-- Use method, not type
dataType: "html",
data: {
action: "login"
},
success: function (data) {
console.log(data)
}
});
Problem is solved.....
There is a problem with the callback on success event...and yesterday I focused my attention only on the request php in the developers tool....when I used Curl (with the right request) all work correctly and I forgot to check client code in success callback....
Never use post method with developers console,it's very easy to lose more time :=)

Knockout and AJAX post request PHP

I am attempting to get our knockout form to submit to a php script and am getting undefinedIndex errors. I am pretty sure it is the way we are sending the data over in our ajax function.
Here is the ajax:
$.ajax({
url: '/orders/add',
type: 'post',
data: {payload:ko.toJSON(allModel)},
contentType: 'application/json',
success: function (result) {
alert(result);
}
});
Here is the PHP (we use laravel)
return json_decode($_POST["payload"]);
Pete is correct. You need to use just one data field. If you want a variable, define it before the $.ajax post
var dataPayload = ko.toJSON(allModel);
$.ajax({
url: '/orders/add',
type: 'post',
data: {payload: dataPayload},
contentType: 'application/json',
success: function (result) {
alert(result);
}
});

Jquery - Send variables to a controller Action via GET in AJAX

All,
I want to send a variable "itemId" via GET to a controller action through AJAX. In the Controller Action, I should be able to retrieve the value using $_GET["itemId"];
Can I send the querystring with "data" tag instead of appending it to the "url"?
I have the following code:
$.ajax({
type: 'GET',
url: "/controller/controlleraction",
data: itemId,
cache: false,
dataType: "html",
success: function(html_input)
{
alert(html_input);
}
});
How can I do this?
data: {itemId: itemId},
$.ajax({
type: 'GET',
url: "/controller/controlleraction",
data: ({itemId: itemId}),<------change it to this
cache: false,
dataType: "html",
success: function(html_input)
{
alert(html_input);
}
});
Make itemId a JavaScript object before making the AJAX request. For example:
var itemId = {'itemId': 1000};
data: {itemId: "you info"},
or
data: "itemId=you info",

Categories