jQuery ajax drops integer on url

大城市里の小女人 提交于 2019-12-24 01:29:14

问题


I am using this data...

 // Pulled from the button that was hit
 var method = document.activeElement.getAttribute('method').toUpperCase(); 
 var url = document.activeElement.getAttribute('url');

Both buttons (methods) have the same url defined... '/user/1'

This is my jQuery AJAX call...

 $.ajax({
   type: method,
   url: url',   /* I even hard coded this to make sure what it had to use */
   contentType: "application/json; charset=utf-8",
   data: $(form).serializeArray(),
   error: function (xhr, status) {
     alert(xhr.status + ' : ' + url);
   }
 });

I have discovered that a PUT call truncates the number at the end of the given URL. But it has a trailing SLASH, it's OK.

If I run DELETE, it does what I expect

DELETE: /user/1

If I run PUT, I don;t get what I expect...

PUT: /user

notice the number "1" missing from that url?

I dug around and decided to try native JS... this is what I have...

 var xhr = new XMLHttpRequest();
 xhr.open(method, url);
 xhr.setRequestHeader('Content-Type', 'application/json');
 xhr.onload = function() {
   if (xhr.status === 200) {
     var userInfo = JSON.parse(xhr.responseText);
   }
 };
 xhr.send(JSON.stringify($(form).serializeArray()));

If I run DELETE, it does what I expect

DELETE: /user/1

If I run PUT...

PUT: /user/1

It works.

Yes, I know the URL has the "1" at the end for a PUT because I am sending the variable to the console before AND after the ajax call.

console.log(method + ': ' + url);

Besides, its the same var creation used for the native JS as used for the jQuery.

Now here's the kicker!

If the URL is defined as...

PUT: /user/walter

The name at the end stays.

Any ideas?

来源:https://stackoverflow.com/questions/50084760/jquery-ajax-drops-integer-on-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!