I am trying the following code for getting subcategory from the category field that I was selected using ajax. But the value is not passed to the controller function. I tried every solution and but still does not work.
$.ajax({
url: "echo base_url().'design/get_sub_category';",
traditional: true,
data: JSON.stringify(id),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data);
}
});
If I have understood correctly you probably want this:
$.ajax({
url: "<?php echo base_url(); ?>/design/get_sub_category",
traditional: true,
data: JSON.stringify(id),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data);
}
base_url() is a PHP function and you need to echo the output as <?php echo base_url() ?>.
Related
I have this working .getJSON
$.getJSON('data3.json', function (data) {
})
If convert to $.ajax What should be the value for data?
$.ajax({
type: 'GET',
url: 'data3.json',
datatype: 'json',
data: ?,
success: function(data) {
}
});
You should try this
In the ajax, they are not compulsory to pass some parameters like data, datatype etc.
So you can call ajax without data parameter
$.ajax({
type: 'GET',
url: 'data3.json',
datatype: 'json',
success: function(data) {
}
});
$.ajax({
dataType: "json",
type: "POST",
url: "<?php echo Router::url(array('controller' => 'useroccupations', 'action' => 'savedJobDelete')); ?>'",
data: ({type:'original'}),
success: function (data){
$('#menu1').tab('show');
}
});
Here is my ajax and It's not working correctly . Can I get best advice form you ?
$.ajax({
dataType: "json",
type: "POST",
url: "<?php echo Router::url(array('controller' => 'useroccupations', 'action' => 'savedJobDelete')); ?>",
data: ({type:'original'}),
success: function (data){
$('#menu1').tab('show');
}
});
What I did is removing extra single quote in url savedJobDelete')); ?>'",
I am having a problem while using cross-origin ajax.
I know it's a common question but did not get any solution for it yet.
$.ajax({
type: "GET",
url: url,
data: {id:id},
dataType: "jsonp",
crossDomain: true,
contentType: "application/jsonp; charset=utf-8",
async: false,
success: fnsuccesscallbackk,
error: function(xhr, error){
alert(error);
},
jsonpCallback: fnsuccesscallback
});
function fnsuccesscallback(data){
alert(data)
}
…but getting undefined response in callback function.
Is there anything wrong what I am doing.
After a lots of RND finally i got the solution of this.
Ajax function:
$.ajax({
type:"GET",
url:'https://www.url.com/welcome/test_js',
data:{name:'xyz'},
crossDomain:true,
dataType: "jsonp",
jsonp: 'fnsuccesscallback',
success: function(data) {
alert(data.name)
}
});
In the Php function:
function test_js() {
echo $_GET['fnsuccesscallback'] . "(" . json_encode($_GET) . ")";
}
I am trying to call to a different document to get the phone number. Unfortunately, it does not appear to be working properly and won't pull the correct phone number. Is there a way I can just set the phone number in the code? Currently my code looks like this:
function setTollNo() {
$.ajax({
type: "POST",
url: "/PURLService.asmx/GetTollNo",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(html) {
$("[id*=tollNo]").html(html.d);
},
error: function() { alert('error'); }
});
}
Would I be able to put something along the lines of the following?
tollNo = 18005557755
I think you are looking for
function setTollNo() {
$.ajax({
type: "POST",
data: { tollNo: '18005557755' },
url: "/PURLService.asmx/GetTollNo",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(html) {
$("[id*=tollNo]").html(html.d);
},
error: function() { alert('error'); }
});
}
Go to the jQuery.ajax docs and search for "Examples".
I have this:
public ActionResult GetBear(int bearId)
{
return Json(bear);
}
Here is the ajax call to it:
$.ajax({ url: "correctUrl", dataType: 'json', data: { bearId: 2}, contentType: "application/json; charset=utf-8", success: function (data) {
alert("something")
}
GetBear gets executed, but the success method is not entered. What is the porblem?
I added error field and it says Internal server error. Why? I'm not comunicating with a server.
Try this, check if you get alert or not ?
$.ajax({
type: "POST",
url: "correctUrl",
dataType: 'json',
data: {bearId: 2},
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("something")
},
error: function(data) {
//AJAX request not completed
alert("it shows error");
}
And check if page(correctUrl) exist on which you send ajax request..
First check URl Since you wrote function executing
change your function return type from ActionResult to JsonResult