Wednesday 22 February 2017

Javascript Example To Call an Ajax

Example code in javascript to call an ajax

function changeDeliveryTime() {

    var deliveryMethod = $('#deliveryMethod').val();
    var delAddress = $('#delAddress').val();
    var schedule = $('#scheduleko').val();
 $.ajax({
  type : "GET",
  url : "/ajaxchangedeliveryschedule.do?deliveryMethod=" + deliveryMethod + "&address=" +  delAddress + "&scheduleId=" + schedule,         
  success : function(response) {
   //successfully change the time
   //alert('successfully change the time');   
  },
  error : function(e) {
   alert('Error changing time schedule: ' + e);
  }
 });
}

Share:

4 comments:

  1. another method is

    $.get("/basketcount", function (data) {
    console.log('hello')
    console.log(data);
    console.log('hello')
    }, "json"); // or xml, html, script, json, jsonp or text

    ReplyDelete
  2. Another method

    var paramObj = new Object();
    paramObj.id = currentInput.attr('id');
    paramObj.field = currentInput.attr('field');
    paramObj.newvalue = newValue;

    $.ajax({
    method: 'POST',
    url: '/customervalueedit',
    data: paramObj
    }).done(function (result) {
    $.simplyToast("Changes saved successfully", 'success');
    }).error(function (error) {
    $.simplyToast("Error in editing description: " + error.message, 'warning');
    });

    ReplyDelete
  3. longer method

    var url = '/customervalue';

    var posting = $.post(url,
    {
    customer_id: customerId,
    title: $('#' + secId + 'value').val(),
    description: $('#' + secId + 'desc').val(),
    templatecomponent_id: valueId,
    sort_order: $('#' + secId + 'order').val(),
    });


    posting.done(function (data) {
    console.log(data);
    $.simplyToast('Saved successfully', 'success');
    }).fail(function (err) {
    console.log(err);
    $.simplyToast('Error in saving record', 'error');
    });

    ReplyDelete
  4. Rails controller

    begin

    tempvalue = Templatecomponentvalue.new
    tempvalue.update_attributes(values_params)
    tempvalue.save

    render:json => {:message => 'Saved successfully'}, :status => 200
    rescue => ex

    render:json => {:errors => ex.message}, :status => 422
    end

    ReplyDelete

Popular Posts

Recent Posts

Pages

Powered by Blogger.

About Me

My photo
For the past 10 years, I've been playing with codes using PHP, Java, Rails. I do this for a living and love new things to learn and the challenges that comes with it. Besides programming I love spending time with friends and family and can often be found together catching the latest movie or planning a trip to someplace I've never been before.