简体   繁体   English

用ajax和jquery初始化,用$ get麻烦

[英]initiate with ajax and jquery, troubles with $get

I don't understand this, I have this code: 我不明白,我有以下代码:

<html>
<head>
<title>Problema</title>
<script language="javascript" type="text/javascript" src="jquery-1.7.min.js"></script>
</head>
<body>
<script language="javascript">

$.get("hola.html", function(respuestaSolicitud){
   alert(respuestaSolicitud);
}) 

</script>
</body>
</html>

The file hola.html contains only a string "hola hola". 文件hola.html仅包含字符串“ hola hola”。 The alert is showing this string: [Object XMLDocument], according a manual this should show an alert with "hola hola" text. 警报显示以下字符串:[Object XMLDocument],根据手册,该警报应显示带有“ hola hola”文本的警报。

I think this page will help you: http://api.jquery.com/jQuery.get/ 我认为此页面将为您提供帮助: http : //api.jquery.com/jQuery.get/

And particularly this paragraph: 特别是本段:

The success callback function is passed the returned data, which will be an XML root element, text string, JavaScript file, or JSON object, depending on the MIME type of the response . 成功回调函数将传递返回的数据,该数据将是XML根元素,文本字符串,JavaScript文件或JSON对象, 具体取决于响应的MIME类型 It is also passed the text status of the response. 它还传递了响应的文本状态。

Try this: 尝试这个:

$.ajax({
  type: "GET",
  url: "hola.html",
  contentType: "text/plain; charset=utf-8",
  success: function(data, textStatus){
    alert(data);
  },
  error: function(data){
    alert("error");
  }
});

If you are expecting HTML to be returned from the AJAX request, you need to specify it in the datatype parameter. 如果期望从AJAX请求返回HTML,则需要在datatype参数中指定它。 jQuery is good at guessing the returned datatype, but can get confused between XML and HTML when only returning a portion of a HTML page. jQuery擅长猜测返回的数据类型,但是当仅返回HTML页面的一部分时,可能会使XML和HTML混淆。

$.get(
   "hola.html", 
    function(respuestaSolicitud) {
        alert(respuestaSolicitud);
    },
    dataType: "html"
);

Try getJSON instead. 尝试改用getJSON。 It seems the best for this case. 对于这种情况,这似乎是最好的。

$.getJSON("test.html", function(json){ alert(json); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM