zoukankan      html  css  js  c++  java
  • Jquery.ajax中dataType不可少

    官方关于这个参数的解释:

    dataType (default: Intelligent Guess (xml, json, script, or html))
    Type: String
    The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:
    • "xml": Returns a XML document that can be processed via jQuery.
    • "html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.
    • "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to trueNote: This will turn POSTs into GETs for remote-domain requests.
    • "json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)
    • "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.
    • "text": A plain text string.
    • multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

    php文件如下:

    <?php
    header("Content-Type:text/html;charset=utf-8");

    echo json_encode(array("hello","world");

    ?>

    $.ajax({
    url:"lib3/grabBaiduBaike.php",
    type:"GET",

    data:{q:queryValue},
    error: function(){
    alert('载入数据失败,服务器可能遇到故障');
    },
    beforeSend:loading,
    success:function(data){ 

    console.log(data);
    var str1=data[0];
    var str2=data[1];
    console.log(typeof data);
    console.log(str1);
    console.log(str2);

    }
    });

    你会发现str1是h,str2为e,与想象的结果不符,这是为什么。

    因为php的header中已经说明这是text/html类型,jquery把返回过来的数据就当做字符串串来处理了,加上:

    dataType:'json',

    就正常了。

  • 相关阅读:
    macbook使用美化工具在屏幕展示出常查信息
    Mac 下 Docker 运行较慢的原因分析及个人见解
    Json Web Token
    Mac开发环境部署
    python3.6_多进程_multiprocessing.pool_concurrent.futures_ProcessPoolExecutor_对比
    apache2_nginx_反向代理设置_https_不验证后端证书_只允许访问首页
    awstats_yum_dnf_centos8_nginx_X-Forwarded-For
    Django_uwsgi_nginx_centos_笔记
    python3_pandas.HDFStore_h5py_HDF5_的笔记
    echarts_highcharts_笔记_风向箭头
  • 原文地址:https://www.cnblogs.com/youxin/p/2938620.html
Copyright © 2011-2022 走看看