简体   繁体   English

无法获得$ .ajax或$ .get上班

[英]Can't get $.ajax or $.get to work

I have this $.ajax (using jquery) code, it originally was the $.get that is now commented but for some reason I'm always getting the error and I can't find anything wrong with it =/, am I overlooking something? 我有这个$ .ajax(使用jquery)代码,它原来是$ .get,现在已注释,但是由于某种原因,我总是遇到错误,我找不到任何错误= /,我是否在忽略什么东西

$.fn.randomContent = function(options){
    var contentArray = new Array();
    var dType = "html";
    var defaults = {
        xmlPath: "../xml/client-quotes.xml",
        nodeName: "quote"
    };
    var options = $.extend(defaults, options);
    alert(options);
    $.ajax({
        type: "GET",
        url: "../xml/client-quotes.xml",
        dataType: "html",
        success: function(){
            $(defaults.nodeName).each(function(i){
                contentArray.push($(this).text());
            });
            $(this).each(function(){
                $(this).append(getRandom());
            });
        },
        error: function(){
            alert("Something Went wrong");
        }

    });
    /*$.get(defaults.xmlPath, function(){
                                         alert("get");
        $(defaults.nodeName).each(function(i){
            contentArray.push($(this).text());
        });
        $(this).each(function(){
            $(this).append(getRandom());
        });
    }, type);//$.get*/
};

Here's the getRandom() function: 这是getRandom()函数:

function getRandom() {
    var num = contentArray.length
    var randNum = Math.floor(Math.random()*num)
    var content = "";
    for(x in contentArray){
        if(x==randNum){
            content = contentArray[x];
        }
    };
    alert(content);
    return content;
}

It could be that the browser is caching your GET requests. 可能是浏览器正在缓存您的GET请求。 In this case, either: 在这种情况下,请执行以下任一操作:

  1. ensure the server is controlling your cache options (using cache-control settings of private or no-cache) 确保服务器正在控制您的缓存选项(使用私有缓存或无缓存的缓存控制设置)
  2. change the method of your AJAX call to POST instead of GET 将您的AJAX调用方法更改为POST而不是GET
  3. differentiate your GET request by adding querystring parameters that change with each request 通过添加随每个请求而变化的querystring参数来区分GET请求

I prefer option #1, specifically because POST operations are intended to change something on the server, and so we should use that method when our actions do in fact modify server state. 我更喜欢选择#1,特别是因为POST操作旨在更改服务器上的某些内容,因此,当我们的操作实际上确实在修改服务器状态时,我们应该使用该方法。 GET requests on the other hand, are requests that do nothing more than read data. 另一方面,GET请求是只读取数据而无所事事的请求。

I feel a GET request is more appropriate for this task, and so in my own code I would prevent the caching of the response. 我觉得GET请求更适合此任务,因此在我自己的代码中,我将防止缓存响应。

Does that url have to be absolute? 该网址必须是绝对的吗? I've never tried doing ajax requests with a "../" in it. 我从未尝试过在其中带有“ ../”的ajax请求。 Do you have FireBug installed? 您安装了FireBug吗? You could examine the response header from the sever. 您可以从服务器检查响应头。

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

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