简体   繁体   English

在jQuery中使用ajax - 不接受url

[英]Using ajax with jQuery - url not accepted

I'm trying to build an ajax request that will eventually allow users to sort a list by clicking buttons next to each item. 我正在尝试构建一个ajax请求,最终允许用户通过单击每个项旁边的按钮对列表进行排序。 Here's what I have so far: 这是我到目前为止所拥有的:

upLinks.on('click', function(e) {
  var link;
  e.preventDefault();
  link = $(this)[0];
  console.log(link.pathname);
  $.ajax({
    type: 'PUT',
    url: link.pathname
  });
  return false;
});

console.log(link.pathname) logs out /projects/11/project_items/104/sort to the console, as expected. console.log(link.pathname)按预期注销/projects/11/project_items/104/sort到控制台。 However, the ajax request returns an error: 但是,ajax请求返回错误:

PUT http://localhost:3000/projects/11 400 (Bad Request)

Looking at the server logs, it is in fact going to /projects/11 , not /projects/11/project_items/104/sort for some reason. 查看服务器日志,它实际上是出于某些原因转到/projects/11 ,而不是/projects/11/project_items/104/sort According to the jQuery docs, /projects/11 would be the default parameter for ajax in this case, because that's the route of the current page. 根据jQuery文档,在这种情况下, /projects/11将是ajax的默认参数,因为这是当前页面的路由。 Which leads me to believe it's ignoring the url parameter altogether. 这让我相信它完全忽略了url参数。

If I change HTTP method type to GET , the ajax runs as expected. 如果我将HTTP方法类型更改为GET ,则ajax将按预期运行。 Is there some reason /projects/11/project_items/104/sort is not an acceptable url for a PUT request? 是否有某些原因/projects/11/project_items/104/sort不是PUT请求的可接受URL? I thought that because I'm updating data (with the new position) a PUT request would be the most logical option. 我认为,因为我正在更新数据(使用新位置), PUT请求将是最合理的选择。

Just a note, if you're using an IIS webserver and the jquery PUT or DELETE requests are returning 404 errors, you will need to enable these verbs in IIS. 请注意,如果您使用的是IIS Web服务器并且jquery PUT或DELETE请求返回404错误,则需要在IIS中启用这些谓词。 I've found this to be a good resource: http://geekswithblogs.net/michelotti/archive/2011/05/28/resolve-404-in-iis-express-for-put-and-delete-verbs.aspx 我发现这是一个很好的资源: http//geekswithblogs.net/michelotti/archive/2011/05/28/resolve-404-in-iis-express-for-put-and-delete-verbs.aspx

"The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers." “要求的类型(”POST“或”GET“),默认为”GET“。注意:此处也可以使用其他HTTP请求方法,如PUT和DELETE,但并非所有浏览器都支持它们“。 from: http://api.jquery.com/jQuery.ajax/#options 来自: http//api.jquery.com/jQuery.ajax/#options

Reference stack question: How to send a PUT/DELETE request in jQuery? 参考堆栈问题: 如何在jQuery中发送PUT / DELETE请求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM