简体   繁体   English

Ajax 调用没有返回任何内容

[英]Ajax call isn't returning anything

I'm pretty much a complete beginner at javascript and jQuery, so please bear with me.我几乎是 javascript 和 jQuery 的完全初学者,所以请耐心等待。

I have a Spark-API running, and a web front-end that uses it through ajax calls.我有一个 Spark-API 正在运行,以及一个通过 ajax 调用使用它的 Web 前端。

I'm trying to call this function我正在尝试调用此函数

function getSpotifyURL(ms, name) {
        $.ajax({
            url: "http://localhost:8081/playlist?ms=" + ms + "&name=" + name,
            dataType: "json",
        })
        .done(function( data ) {
            console.log(data);
        })
    }

The method is placed outside of:该方法放置在:

$(document).ready(function() {

The reason it's outside is that I get an error upon calling it saying it's "undefined" if it's within $(document).ready.它在外面的原因是,如果它在 $(document).ready 内,我在调用它时会出错,说它是“未定义的”。

The Spark-method is supposed to return a String ( and it does when you try it directly through the browser). Spark 方法应该返回一个字符串(当您直接通过浏览器尝试它时它会返回)。

The way I'm calling the getSpotifyURL-method is through a html button's "onclick".我调用 getSpotifyURL 方法的方式是通过 html 按钮的“onclick”。 Like this:像这样:

<a href='#' onclick='getSpotifyURL(" + data[i].duration + ",\"" + data[i].destination + "\")'>Create Spotify playlist for this trip</a>"

The problem: The .done-block does nothing in my code.问题: .done-block 在我的代码中什么都不做。 Nothing is printed to console.没有任何内容打印到控制台。

What I've tried:我试过的:

  • Using "success" within the ajax part instead of .done在 ajax 部分中使用“success”而不是 .done
  • Placing the function with $(document).ready(function() { ... }用 $(document).ready(function() { ... } 放置函数

I understand that you might need more information to be able to help me, but I'm not sure what else information to provide right now.我知道您可能需要更多信息才能为我提供帮助,但我不确定现在还需要提供哪些信息。 So if there's something you need to know, just ask.所以如果有什么你需要知道的,尽管问。

Ideas?想法?

SOLVED!解决了!

I'm a dumb person and forgot to remove dataType: "json", as the Spark-server in this instance returned a String, not a json object.我是一个愚蠢的人,忘记删除 dataType: "json",因为这个实例中的 Spark-server 返回了一个字符串,而不是一个 json 对象。 Anyway, thanks for your input everybody.不管怎样,谢谢大家的意见。 Much appreciated.非常感激。

I think the problem is when you bind your function onclick.我认为问题是当你绑定你的函数 onclick 时。 There is a syntax error, as you can see on the browser console存在语法错误,正如您在浏览器控制台上看到的

 function getSpotifyURL(ms, name) { console.log("http://localhost:8081/playlist?ms=" + ms + "&name=" + name); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href='#' onclick='getSpotifyURL(" + data[i].duration + ",\\"" + data[i].destination + "\\")'>Create Spotify playlist for this trip</a>"

I guess data is a variable, so you should call it without brackets我猜data是一个变量,所以你应该不带括号调用它

<a href='#' onclick='getSpotifyURL(data[i].duration, data[i].destination)'>Create Spotify playlist for this trip</a>

The reason you are getting undefined method when you are placing the function inside the $(document).ready(function() { ... });当您将函数放入$(document).ready(function() { ... });时,您得到undefined method的原因$(document).ready(function() { ... }); call is because you are using the onclick attribute to call the function. call 是因为您正在使用onclick属性来调用该函数。 $(document).ready(...) is not in global context as to where you onclick attribute is, and would therefore not recognize it from within the document.ready resulting in your undefined method error. $(document).ready(...)不在全局上下文中,因为您的onclick属性所在的位置,因此不会从 document.ready 中识别它,从而导致您undefined method错误。

When sending your Ajax request, you also need to specify a type of request ( GET or POST ) that you are making.在发送 Ajax 请求时,您还需要指定您正在发出的请求类型( GETPOST )。 I would also suggest restructuring your ajax call to look more like @Moe's answer.我还建议重组您的 ajax 调用,使其看起来更像@Moe 的回答。

If you want to get it inside the DOM, consider doing the following:如果您想将其放入 DOM,请考虑执行以下操作:

HTML HTML

<!-- I gave the link a class name and removed the onclick= attribute -->
<a href="#" class="create-spotify-playlist">Create Spotify playlist for this trip</a>

JavaScript JavaScript

$(document).ready(function() {

    // I gave the <a> link a click handler
    $(".create-spotify-playist").on("click", function(e) {
        e.preventDefault();  // prevents link from requesting

        var ms = ??   //I'm not sure where you're getting your parameters from,
        var name = ?? //so you will probably have to figure out how to get those in here yourself

        $.ajax({
            type: "GET",
            url: "http://localhost:8081/playlist",
            data: { ms: ms, name: name },
            success: function(data) {
                console.log("Success: " + data);
            },
            error: function(data) {
                console.log("Error: " + data);
            }
        });
    });
});

I gave the link a click handler and placed it inside the $(document).ready , and by removing the onclick attribute from earlier, it can now be triggered from inside $(document).ready .我为链接提供了一个点击处理程序并将其放置在$(document).ready ,通过删除之前的onclick属性,现在可以从$(document).ready内部触发它。

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

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