i want a url like this , http://localhost/myproject/get-dishes/1
here is my ajax request code
$('#MainDropDown').change(function(e){
var cid = $(this).val();
e.preventDefault();
$.getJSON("{{url('get')}}/"+cid+" ",
function(data) {
var model = $('#dishDropDown');
model.empty();
$.each(data, function(index, element) {
model.append("<option value='"+element.id+"'>" + element.sub_cate_name + "</option>");
});
});
});
expected result:
http://localhost/teraso/public/get-dishes/1
showing:
http://localhost/teraso/public/add/%7%get-dishes/%D1
Try another alternative way like route() or to() function.
try this..
let url = "{{route('get',[':id-url'])}}";
url = url.replace(':id-url', $(this).val());
then, parse it to ajax
$.getJSON(url, function(data) {
var model = $('#dishDropDown');
model.empty();
$.each(data, function(index, element) {
model.append("<option value='"+element.id+"'>" + element.sub_cate_name + "</option>");
});
});
Related
I want to clear my previous ajax response images before appending new images please help me how can i do that ? thank u.
Ajax
<script>
$(document).ready(function(){
var preloaded = [];
$(document).on('click','.editBtn',function(){
var marchandiseId = $(this).data("id");
var file ="{{Config('wfh.file')}}";
$(".input-images-edit").empty();
$("#editModel").modal('show');
var url = "{{URL('/marchandise/edit/')}}";
$.ajax({
type:"GET",
url: url + '/' + marchandiseId,
dataType:'JSON',
success:function (response){
var data = response.marchandise.marchandise_details;
$.each(data, function(i, item) {
preloaded.push({id: item.id, src: file + '' + item.images });
});
$('.input-images-edit').imageUploader({
preloaded: preloaded,
imagesInputName: 'photos',
preloadedInputName: 'old'
});
}
});
});
Here's a very simple jQuery example :
http://www.ihaols.com/geo.html
Code :
$(function(){
$('#btn').click(function(){
var address = encodeURIComponent($('#k').val());
$.get('http://maps.google.com/maps/api/geocode/json?address='+ address + '&sensor=false', function(data){
console.log(data);
var l = data.results[0].geometry.location;
var v = l.lat + ',' + l.lng;
$('#r').val(v);
});
});
});
But this code doesn't work in IE7/8, I've already include json2.js in it.
sorry for this request but I'm an AJAX beginner.
I have the following script and I'm trying to animate it (something like "fade").
jQuery(document).ready(function($){
$(document).on("click",".ratingemo", function(){
var rating = $(this).attr("id").substr(0, 1);
var id = $(this).attr("id").substr(1);
var data = "id="+id+"&rating="+rating;
$.ajax({
type: "POST",
url: "/ldplug/rate.php",
data: data,
success: function(e){
$("#r"+id).html(e);
}
})
});
});
How can I do that?
Many thanks!
First of all live is deprecated, check out on instead. But that being said, why not:
success: function(e){
$("#r"+id).hide();
$("#r"+id).html(e).fadeIn("slow");
}
It would be better to just have $("#r"+id) hidden to start, I just hid it to illustrate the point.
Without seeing your HTML, I have to assume that something like this will work for you. Hide the element, populate it, then fade it in.
jQuery(document).ready(function ($) {
$(".ratings").live("click", function () {
var rating = $(this).attr("id").substr(0, 1);
var id = $(this).attr("id").substr(1);
var data = "id=" + id + "&rating=" + rating;
$("#r" + id).hide();
$.ajax({
type: "POST",
url: "/ratings/rate.php",
data: data,
success: function (e) {
$("#r" + id).html(e);
$("#r" + id).fadeIn();
}
})
});
});
I have a delegate function of JQuery to remove a row of a grid which is anchor tagged remove, as below
$('#MyGrid').delegate('a.remove', 'click', function() {
// e.preventDefault();
var tr = $(this).closest('tr');
var htmlstring = tr.find("td").eq(0).html();
alert(htmlstring);
jQuery.ajax( //$.ajax( also not working
{
type: "POST",
url: "UploadDocuments/Removefile",
data: "removefile=" + htmlstring,
success: function(){
tr.remove();
}
});
});
this is where i am making delegate call in success function of JQuery
success: function(result) {
var htmlString = [result];
for (i = 0; i < htmlString.length; i++) {
$('#MyGrid tbody').append('<tr><td>Remove</td></tr>');
}
},
Now i want to make ajax call as shown but it is not hitting once i click remove ,but is loading initially.Also i need to pass the data i.e the name of the deleted row.How can i get that value?
could any of you guys help me out! got stucked !! ;)
thanking you,
michaeld
Try this where IndexOfNameColumn points to the name column index.
$('#MyGrid').delegate('a.remove', 'click', function() {
// e.preventDefault();
var tr = $(this).closest('tr'); //line#3
var htmlstring = tr.find("td").eq(IndexOfNameColumn).html();
jQuery.ajax( //$.ajax( also not working
{
type: "POST",
url: "UploadDocuments/Removefile",
data: "removefile=" + htmlstring //file name from line#3
});
tr.remove();
});
try this code-
$('#MyGrid').delegate('a.remove', 'click', function() {
// e.preventDefault();
var tr = $(this).closest('tr').find("td").eq(0).find('a').text().replace(/"/g,"").trim(); //line#3
var $this = $(this);
jQuery.ajax(
{
type: "POST",
url: "UploadDocuments/Removefile",
data: "removefile=" + tr //file name from line#3
success: function(){
$this.closest('tr').remove();
}
});
});
How do i update textbox using MVC Ajax UpdateTargetId option.?
I am new to MVC Ajax applications. Please any one help me out.
Thanks,
Pon Kumar Pandian .T
you can achieve that simple concept using
1- Post\Get
2- Ajax
Note: just replace the $("#myform").html() with $("#mytext").text = "data"
1- Using Get\Post
/////// Controller post and get simple text value
[HttpPost]
public string Contact(string message)
{
return "<h1>Hi,</h1>we got your message, <br />" + message + " <br />Thanks a lot";
}
//// in the view add reference to the Javascript (jQuery) files
#section Scripts{
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<script src="~/Scripts/jquery-1.8.2.intellisense.js"></script>
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
}
/// then add the Post method as following:
<script type="text/javascript">
/// post and get text value
$("#send").on("click", function () {
$.post('', { message: $('#msg').val() })
//// empty post('') means post to the default controller,
///we are not pacifying different action or controller
/// however we can define a url as following:
/// var url = "#(Url.Action("GetDataAction", "GetDataController"))"
.done(function (response) {
$("#myform").html(response);
})
.error(function () { alert('Error') })
.success(function () { alert('OK') })
return false;
});
</script>
2-Also you can use Get only as following:
/////Controller = Home
//// Action = FullName
///// get only simple text message
[HttpGet]
public string FullName()
{
return "full Name: Mahmoud Sayed";
}
/// in the view:
$("#getfullname").on("click", function () {
var url = "#(Url.Action("FullName", "Home"))"
$.get(url, { })
.done(function (response) {
$("#myform").html(response)
})
.error(function () { alert('Error') })
.success(function () { alert('OK') })
return false;
});
</script>
3- Now let's say you want to do it using $.Ajax and JSON:
// Post JSON data add using System.Net;
[HttpPost]
public JsonResult JsonFullName(string fname, string lastname)
{
var data = "{ \"fname\" : \"" + fname + " \" , \"lastname\" : \"" + lastname + "\" }";
//// you have to add the JsonRequestBehavior.AllowGet
//// otherwise it will throw an exception on run-time.
return Json(data, JsonRequestBehavior.AllowGet);
}
Then, inside your view: add the event click on a an input of type button, or even a from submit:
Just make sure your JSON data is well formatted.
$("#jsonGetfullname").on("click", function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "#(Url.Action("JsonFullName", "Home"))",
data: "{ \"fname\" : \"Mahmoud\" , \"lastname\" : \"Sayed\" }",
dataType: "json",
success: function (data) {
var res = $.parseJSON(data);
$("#myform").html("<h3>Json data: <h3>" + res.fname + ", " + res.lastname)
},
error: function (xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
})
});
</script>
cheers,
Mahmoud Sayed
You can simply wrap all you want to update in some div with id same that you're putting in UpdateTargetId property, and return new content(new textbox with new value). And also don't forget to set Ajax updating type to replace(not append).
We can't give the textbox control id directly in UpdateTargetId property. Bur we can do work around to achieve this. Please fine the blow mention code for the same.
//This the call back method once the ajax request has been successes.
function fnOnSuccess(context) {
alert(context); //this context having all the current ajax req & res information.
var response = context.get_data(); //Using this get_data() method we can get the response from server.
$('#txtajaxResponse')[0].value = response;
}
<!-- Code in .aspx page -->
<%=Ajax.ActionLink("TextBox in UpdateTargetId","GetInfo","Ajax",new AjaxOptions{OnSuccess = "fnOnSuccess"}) %>
<%=Html.TextBox("txtajaxResponse")%>
Thanks,
Pon Kumar Pandian .T