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',

    就正常了。

  • 相关阅读:
    c#中子线程控制进度条的一个简单例子
    c#中WinForm的TextBox循环自动滚动示例
    c#中重定向windows控制台程序的输出信息
    c#中List <int[]>集合添加和查找元素
    LogExplore的一个详细操作手册
    ConsoleApplication也精彩,一个控制台进度条的示例。
    c#通过oledb获取excel文件表结构信息
    Dephi中获取webbrowser选取区域的html代码示例
    SQL2005中利用xml拆分字符串序列
    今天装上了php6```
  • 原文地址:https://www.cnblogs.com/youxin/p/2938620.html
Copyright © 2011-2022 走看看