Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在回调中使用时如何更改ajax url_Javascript_Jquery - Fatal编程技术网

Javascript 在回调中使用时如何更改ajax url

Javascript 在回调中使用时如何更改ajax url,javascript,jquery,Javascript,Jquery,如何将url从/devices/cars更改为/devices/trucks?我更改了autocomplete的源代码,但原则上,我不希望重复回调 $(function(){ $("#autofield" ).autocomplete({ source: function( request, response ) { $.ajax({ dataType: "json", url: "/

如何将url从/devices/cars更改为/devices/trucks?我更改了autocomplete的源代码,但原则上,我不希望重复回调

$(function(){
    $("#autofield" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                dataType: "json",
                url: "/devices/cars",
                data: {term:request.term, type:'all', fields:['id','name']},
                success: function(json) {
                    var data=[];
                    for (var i = 0; i < json.length; i++) {
                        data.push({id:json[i].id,label:json[i].name});
                    }
                    response(data);
                }
            });
        },
        minLength: 2,
        select: function( event, ui ) {
            $(this).parent().find("input[name='itemId']").val(ui.item.id);
        }
    });

    $("#changeUrl").click(function() {
        //Change url from /devices/cars to /devices/trucks
        $("#autofield" ).val('').autocomplete( "option", "source", function( request, response ) {
            $.ajax({
                dataType: "json",
                url: "/devices/trucks",
                data: {term:request.term, type:'all', fields:['id','name']},
                success: function(json) {
                    var data=[];
                    for (var i = 0; i < json.length; i++) {
                        data.push({id:json[i].id,label:json[i].name});
                    }
                    response(data);
                }
            });
        } );
    });
});

我看到的最简单的方法就是创建一个变量作为你的url,除非我误解了你的问题

var dynamicUrl = "/devices/cars";

$("#changeUrl").click(function() {
    dynamicUrl.val(dynamicUrl.val() == '/devices/cars' ? '/devices/trucks' : '/devices/cars');
});

$.ajax({
...
    url: dynamicUrl,
...

下面是一种基于数组索引更改URL的方法。 这将使您能够使用任意多的URL

请注意,您必须销毁以前的实例,并将其与新URL重新关联

$(function(){

    var ajaxURLs = ["/devices/cars","/devices/trucks", "/devices/planes", "/devices/boats" ];

    var usedURL = 0;  // The array index.

    function autoCompleteInit(){
      $("#autofield" ).autocomplete({
          source: function( request, response ) {
              $.ajax({
                  dataType: "json",
                  url: ajaxURLs[usedURL],
                  data: {term:request.term, type:'all', fields:['id','name']},
                  success: function(json) {
                      var data=[];
                      for (var i = 0; i < json.length; i++) {
                          data.push({id:json[i].id,label:json[i].name});
                      }
                      response(data);
                  }
              });
          },
          minLength: 2,
          select: function( event, ui ) {
              $(this).parent().find("input[name='itemId']").val(ui.item.id);
          }
      });
    }

    // On load init
    autoCompleteInit();

    // Change handler
    $("#changeUrl").click(function() {
        //Change URL to the next URL in array

        usedURL++;

        // Back to first URL if cycled throught all URL
        if(usedURL > ajaxURLs.length-1 ){
          usedURL = 0;
        }

        // Destroy previous autocomplete instance ans re-initialise it with the new URL.
        $("#autofield" ).autocomplete("destroy");
        autoCompleteInit();
    });
});

创建一个包含Ajax调用的函数,并将url或前缀作为参数传递。 喜欢
GetDeviceUrl

是否要在运行时更改它?@ArupRakshit在页面加载后更改它。我认为DynamicCurl不能以这种方式引用。@User1032531为什么不能?我刚刚测试了一个ajax调用,您完全可以将一个变量而不是字符串传递给url参数。是的,使用ajax部分您可以,但是autocomplete的闭包会导致问题。我想我可以每次都重新应用,但是看起来应该可以更精确一些。与其破坏以前的自动完成实例,不如用新的URL重新初始化它,只需更改URL即可。也许只是不可能。最好的办法是尝试。。。我承认我没有测试。。。但我很确定它必须重新实例化。