0

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.

2
  • always accept answer by ticking it as green if you find it helps you Commented May 13, 2018 at 0:24
  • @pradeep Thanks. None of the answers solved my issue... I ended up removing the http:// part of the before sending it and adding it server side. Thats why I didn't checked any of the answers. Commented May 14, 2018 at 19:42

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

What's the difference? Changing name of variable?
There is no difference in changing the variable name. I want to tell you that you do your code in this way. first assign the value in a variable i.e. ID=$(this).data('link'); then send the value on that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.