jQuery jsonp ajax请求的查询字符串中的下划线是什么? [英] What is the underscore for in the query string of a jQuery jsonp ajax request?

查看:364
本文介绍了jQuery jsonp ajax请求的查询字符串中的下划线是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查看来自jsonp请求的查询字符串(下面的客户端代码)时,有2个对象,您需要在响应中使用回调"字符串(因此客户端代码将直接指向成功处理程序),一个键为_的...下划线是做什么用的?我在任何文档中都找不到对此的引用,它似乎是某种形式的数字.

When I look at the query string from a jsonp request (client code below), there are 2 objects, a "callback" string that you need to use in the response (so the client codes directs to the success handler) and one with a key of _... what is this underscore for? I can't find any reference to this in any documentation, it appears to be a number of some kind.

我虽然可以将其用于定向到错误处理程序(在其上,与回调结合使用,或替换回调字符串中下划线后的数字),但似乎没有.

I though that it might be used to direct to the error handler (either on its on, in combination with the callback, or replacing the number after the underscore in the callback string), but it doesn't appear to be.

url = 'http://localhost:11767/Handlers/MyHandler.ashx';

...

$.ajax({
    url: url,
    dataType: "jsonp",
    error: function (jqXHR, textStatus, errorThrown) {
        //...
    },
    success : function(d) {
        //...
    }
});

    $.getJSON(url + "?callback=?", function(d) {
    }).success(function(d) {
        //...
    }).error(function(jqXHR, textStatus, errorThrown) {
        //...
    }).complete(function(d) {
        //...
    });

如果这有助于任何人阅读,请注意:由于这是一个jsonp请求,因此仅当异常发生在客户端(例如,有一个超时或响应格式问题(即不使用回调),为了克服这一点,我总是记录并吞下处理程序中的异常,但给出一个标准响应对象(所有响应均由其组成)具有异常的状态属性和消息的属性.

Side note in case this helps anyone reading this: as this is a jsonp request, error will only be hit if the exception occurs client side, e.g. there is a timeout or a problem with the formatting of response (i.e. not using the callback), to overcome this, I always log and swallow the exceptions in the handlers, but give a standard response object (which all response are made up of) that has a state property of exception and a message property.

推荐答案

您所引用的数字是请求的日期时间戳.抓住数字并使用浏览器的JavaScript控制台,然后键入:alert(new Date(/*insert number here*/))

The number you are referring is the date time stamp of the request. Grab the number and use a your browser's JavaScript console and type: alert(new Date(/*insert number here*/))

您会收到带有日期/时间的警报.

You'll get an alert with a date/time.

以下是 jQuery.ajax 文档中有关ajax请求的摘录:

Here's a snippet from jQuery.ajax doc regarding an ajax request:

缓存
默认值:true,对于dataType'script'和'jsonp'
为false 如果设置为false,将强制浏览器不缓存请求的页面. 将cache设置为false还会附加一个查询字符串参数"_=[TIMESTAMP]", 网址.

cache
Default: true, false for dataType 'script' and 'jsonp'
If set to false, it will force requested pages not to be cached by the browser. Setting cache to false also appends a query string parameter, "_=[TIMESTAMP]", to the URL.

这篇关于jQuery jsonp ajax请求的查询字符串中的下划线是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆