【问题标题】:jquery autocomplete url corruptjquery自动完成网址损坏
【发布时间】:2017-05-26 09:31:33
【问题描述】:

我陷入了奇怪的境地。我正在使用 jquery 的自动完成功能。我已经映射了我的 URL,但我得到了 404。

现在,当我查看控制台时,我的 URL 显示如下:

myProject-dashboard-svc/organization/[object%20Object]

虽然我的实际 URL 类似于 ../organization/suggestion 以下是我的完整 jquery 代码

$(function() {
  $("#searchByText").autocomplete({
    source:function(request,response){
      $.get({
        url:"../organization/suggestion",
        dataType:"json",
        contentType: "application/json",
        data:{
          q:request.term
        },
        success:function(data){
          response(data);
        }
      })
    }
  })
});
<input type="text" id="searchByText" hidden="true" name="searchByText" placeholder="enter name" class="autoComplete">

请告诉我为什么 URL 会这样显示。
顺便说一句,我已经在 Chrome 和 Mozilla 中进行了检查,并且我有 jquery-ui-jQuery-autocomplete 和 jQuery 插件。

【问题讨论】:

  • request.term 是一个对象吗?如果是这样,您需要选择要发送的特定属性。
  • 在自动完成术语中是你传递的对象
  • 好的,但是AJAX请求需要的是字符串,而不是对象
  • 是的,它只是一个字符串格式,请参阅link
  • 这就是我的观点。它应该是一个字符串,但你发送的是一个object

标签: javascript jquery autocomplete jquery-ui-autocomplete


【解决方案1】:

尝试将数据作为字符串而不是 json 对象发送。
;)

$(function() {
  $("#searchByText").autocomplete({
    source:function(request,response){
      $.get({
        url:"../organization/suggestion",
        dataType:"json",
        contentType: "application/json",
        data: {"q:" + JSON.stringify(request.term) },       // Look here!
        success:function(data){
          response(data);
        }
      })
    }
  })
});

【讨论】:

    【解决方案2】:

    您应该签出签名以获得https://api.jquery.com/jquery.get/。 如果你想使用 get 你应该这样做

          $.get("../organization/suggestion", {"q":request.term},function(data){
          response(data);
          },"json");
    

    或者您可以将 $.get 替换为 $.ajax

    【讨论】:

    • 我用 ajax 替换了它,它工作了,但我不明白它有什么不同?
    • $.ajax 函数和 $.get 函数的签名不同。 $ajax 接受一个包含配置的对象,就像你传递它 {url:"...", "data":"...",...} 和 $.get 在单独的参数中接收配置 $.get(url , 数据, 成功函数, 数据类型)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2021-02-15
    • 2010-11-29
    相关资源
    最近更新 更多