zoukankan      html  css  js  c++  java
  • JSONP

    jQuery 中的JSONP

    // Using YQL and JSONP
    $.ajax({
        url: "http://query.yahooapis.com/v1/public/yql",
     
        // The name of the callback parameter, as specified by the YQL service
        jsonp: "callback",
     
        // Tell jQuery we're expecting JSONP
        dataType: "jsonp",
     
        // Tell YQL what we want and that we want JSON
        data: {
            q: "select title,abstract,url from search.news where query="cat"",
            format: "json"
        },
     
        // Work with the response
        success: function( response ) {
            console.log( response ); // server response
        }
    });

    JQ这里的额设置容易引起混淆  dataType是jsonp

    例子来源 https://learn.jquery.com/ajax/working-with-jsonp/

    API http://api.jquery.com/jquery.ajax/

    如果是指定了回调函数名 

    dataType:"jsonp":

    • "jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.

     

    另外还有两个参数可以配置

    • jsonp
      Type: String
      Override the callback function name in a JSONP request. This value will be used instead of 'callback' in the 'callback=?' part of the query string in the url. So {jsonp:'onJSONPLoad'} would result in 'onJSONPLoad=?' passed to the server. As of jQuery 1.5, setting the jsonp option to falseprevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }
    • jsonpCallback
      Type: String or Function()
      Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.

    第一段  如果你的url有指定callback的名字 这里重写了  注意!  xxx=fun  变成了  valueYouSet=fun

    第二段   指定回调函数

  • 相关阅读:
    使用C#直接修改表结构(添加列,删除列)【MS SQL SEVER】
    Nuget-ConsoleExtClass给控制台添加颜色
    Thread线程Join()的使用
    C#将List集合类转换成DataTable-帮助类
    C#动态拼接Linq
    C#使用AutoMapper
    go GOPROXY=http://goproxy.io 设置
    mysql5.6切到5.7(阿里云RDS换到自建库)
    vue学习之----如何在谷歌浏览器中使用vue调试工具
    vue学习之----兄弟组件之间通信方式
  • 原文地址:https://www.cnblogs.com/cart55free99/p/4549199.html
Copyright © 2011-2022 走看看