Cool Guy
Cool Guy

Reputation: 51

Sending url using ajax on codeigniter

I have this Ajax script where I pass a link to the data variable link, but I

get a 412 error.

$(function() {
  $(".check-multi").change(function() {
    $.ajax({
      type: "POST",
      url: "<?php echo site_url('adm/updateproperty'); ?>",
      async: true,
      data: {
        link: $(this).data('link')
      },
      success: function(msg) {
        alert('Success');
        if (msg != 'success') {
          alert('Fail');
        }
      }
    });
  });
});

I have tried

link: encodeURI($(this).data('link'))

And

link: encodeURIComponent($(this).data('link'))

as is suggested on other threads but I still get the 412 error message.

Upvotes: 0

Views: 71

Answers (2)

Pradosh Mukhopadhayay
Pradosh Mukhopadhayay

Reputation: 256

Please change your code in data options. Use this way

data: {"id": ID},

i.e. store the value in a variable and send that variable in data option. If we assume

ID=$(this).data('link');`

Then the code will be as follows:

$(function() {
  $(".check-multi").change(function() {
    $.ajax({
      type: "POST",
      url: "<?php echo site_url('adm/updateproperty'); ?>",
      async: true,
      data: {"id":ID},
      success: function(msg) {
        alert('Success');
        if (msg != 'success') {
          alert('Fail');
        }
      }
    });
  });
});

Please check it.

Upvotes: 1

Pradeep
Pradeep

Reputation: 9707

Hope this will help you :

you have added newline character to json data that is why you got error

Do like this :

var link = $(this).data('link');
data: {"link" : link},

/*----OR do this -----*/

data: {"link" : $(this).data('link')},

instead of this :

data: {
        link: $(this).data('link')
      },

Whole code should be like this :

var link = $(this).data('link');
/*console.log(link)*/
$.ajax({
      type: "POST",
      url: "<?php echo site_url('adm/updateproperty'); ?>",
      async: true,
      data: {"link" : link },
      success: function(msg) {
        alert('Success');
        if (msg != 'success') {
          alert('Fail');
        }
      }
    });

For More :https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/412

Upvotes: 1

Related Questions