2

我想调用 web 服务,直到我得到所需的输出。这是我正在使用的代码。

$.ajax(
{
    type: "POST",
        url: "../WebService/myservice.asmx/GetStatus",
        data: "{'myparameter': '" + value + "'}",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (response) {
                 return response.d;
                 },
        error: function (x, e) {
                alert("The call to the server side failed. " + x.responseText);
                }
});

该网络服务将返回结果。我希望在结果完成之前 jquery 调用应该每 1 秒执行一次。

4

5 回答 5

1

试试这个:

function ajaxCall() {
    $.ajax({
        ...
        success: function (response) {
            //got desired output?
            /*logic*/
            //no?
            setTimeOut(ajaxCall, 1000);
        },
        error: function() {
            setTimeOut(ajaxCall, 1000);
        }
    });
}
于 2013-03-19T07:32:58.033 回答
1

这是一个好的开始

这个想法是将您的代码封装到一个方法中,然后使用setInterval调用它:

setInterval(ajaxCall, 1000);

例如:

function ajaxCall(){
  $.ajax(
  {
      type: "POST",
          url: "../WebService/myservice.asmx/GetStatus",
          data: "{'myparameter': '" + value + "'}",
          contentType: "application/json;charset=utf-8",
          dataType: "json",
          async: true,
          cache: false,
          success: function (response) {
                   return response.d;
                   setInterval(ajaxCall, 1000); //call itself
                   },
          error: function (x, e) {
                  alert("The call to the server side failed. " + x.responseText);
                  setInterval(ajaxCall, 1000); //try again
                  }
  });
}
于 2013-03-19T07:36:28.107 回答
1

使用 jQuery 的Deferred/promise机制的变体:

// interval in ms, use retries = 0 for unlimited retries
function poll(url, data, interval, retries) {
    var result = $.Deferred(), retryCount = 0;

    (function repeat() {
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            contentType: "application/json; Charset=utf-8",
            dataType: "json",
            async: true,
            cache: false
        })
        .then(function (response) {
            if (response.d === "success") {
                result.resolve(response);
            }
        })
        .fail(function (x, e) {
            if (retries === ++retryCount) {
                result.reject("Maximum number of retries reached (" + url + ")");
            } else {
                setTimeout(repeat, interval);
            }
        });
    })();

    return result.promise();
}

用法:

poll(
    "../WebService/myservice.asmx/GetStatus",
    {myparameter: value}, 1000, 100
)
.then(function (response) {
    // ...
})
.fail(function (error) {
    alert(error);
    // ...
})
于 2013-03-19T08:28:11.650 回答
0

将您的 ajax 请求包装在一个命名函数中,您可以在需要发出另一个请求时调用该函数。在成功回调中检查结果,如果对结果不满意,请设置超时以在一秒钟内再次调用该函数。

像这样的东西:

(function Fetch (){
   $.ajax({
        type: "POST",
        url: "../WebService/myservice.asmx/GetStatus",
        data: "{'myparameter': '" + value + "'}",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (response) {
            if (response != "completed") {
                setTimeout(Fetch, 1000);
            } else {
                // Do something when completed 
            }
        },
        error: function (x, e) {
            alert("The call to the server side failed. " + x.responseText);
        }
   });
})();
于 2013-03-19T07:32:10.700 回答
0

正如前面的评论所解释的,如果您希望它每秒(或一段时间)调用一次。您可以使用setTimeout()(函数内部)或setInterval()(外部)。但是,如果您想继续尝试直到获得所需的代码,则最好在函数内部使用循环:

function startRequest() {
    while(1){
        $.ajax(
        {
            type: "POST",
                url: "../WebService/myservice.asmx/GetStatus",
                data: "{'myparameter': '" + value + "'}",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: true,
                cache: false,
                success: function (response) {
                         return response.d;
                         i=0;
                         break;
                         },
                error: function (x, e) {
                        alert("The call to the server side failed. " + x.responseText);
                        }
        });
    }
}
于 2013-03-19T07:48:46.513 回答