简体   繁体   English

JQuery + Ajax:JavaScript运行时错误:预期功能

[英]JQuery + Ajax: JavaScript runtime error: Function expected

I'm new to this and did not find an answer for my problem on the internet. 我是新手,在互联网上找不到我的问题的答案。

I have the following script in my asp.net webpage. 我的asp.net网页中有以下脚本。 It gives me the error that a function is expected. 它给了我一个期望功能的错误。

When I use a hard coded city name and a hard coded div id, it works just fine, But from the moment I start to use a loop to change dynamically 1) the url to be used, 2) the city name and 3) div id, I receive the error. 当我使用一个硬编码的城市名称和一个硬编码的div id时,它就可以正常工作,但是从我开始使用循环来动态更改的那一刻起:1)要使用的网址,2)城市名称和3)div id,我收到错误。 Any solutions? 有什么办法吗? Thanks in advance! 提前致谢!

    jQuery(document).ready(function ($) {

        var strurl;
        var encodedUrl;
        var city;
        var cities = ["firenze", "rome", "milan", "venice", "perugia", "urbino"];
        for (var i = 1; i < 7; ++i) {
            city = cities(i - 1);
            strurl.toString = "http://api.aerisapi.com/observations/" + city + ",it?client_id=ZPYsvZLE4U9tkifhy3XMc&client_secret=IwQPYv7GA9XYR0bc9ziJ03ug5H2Tmh1gmxmAybEd";
            $.ajax({
                url: strurl,
                dataType: "jsonp",
                success: function (json) {
                    if (json.success == true) {
                        var ob = json.response.ob;
                        var weather = ob.weather.toLowerCase();
                        $('#js' + i + '1').html(city + ': ');
                        $('#js' + i + '2').html(weather);
                        $('#js' + i + '3').html(ob.tempC + '°');
                    }
                    else {
                        alert('An error occurred: ' + json.error.description);
                    }
                }
            });
        };            
    });

The problem is here: 问题在这里:

city = cities(i - 1)

It should be: 它应该是:

city = cities[i - 1]

The cities variable is an array, so you access its elements with [] notation - your code used parentheses which means JS tries to treat it as a function call. cities变量是一个数组,因此您可以使用[]表示法访问其元素-您的代码使用括号,这意味着JS尝试将其视为函数调用。

In my opinion it would be tidier to not hardcode the length of the array in your for condition, but use its .length property instead: 我认为最好不要在您的for条件中硬编码数组的长度,而应使用.length属性代替:

for (var i = 1; i <= cities.length; ++i) {
        city = cities[i-1];

And then the next problem you're going to have is that you're trying to use i in your $.ajax() success callback, but each of those $.ajax() calls is asynchronous so by the time the success callbacks run the loop will have finished and i will be 7. The simplest way to fix that is to use jQuery's $.each() method to iterate over your array (instead of a for loop) so that each ajax call is contained in its own function invocation and gets its own i : 然后,下一个问题是您尝试在$.ajax()成功回调中使用i ,但是每个$.ajax()调用都是异步的,因此在运行成功回调时循环将完成, i将是7。最简单的解决方法是使用jQuery的$.each()方法遍历数组(而不是for循环),以便每个ajax调用都包含在自己的函数中调用并获得自己的i

    var cities = ["firenze", "rome", "milan", "venice", "perugia", "urbino"];
    $.each(cities, function(i, city) {
        var strurl.toString = "http://api.aerisapi.com/observations/" + city + ",it?client_id=ZPYsvZLE4U9tkifhy3XMc&client_secret=IwQPYv7GA9XYR0bc9ziJ03ug5H2Tmh1gmxmAybEd";
        $.ajax({
            url: strurl,
            dataType: "jsonp",
            success: function (json) {
                if (json.success == true) {
                    var ob = json.response.ob;
                    var weather = ob.weather.toLowerCase();
                    i++;
                    $('#js' + i + '1').html(city + ': ');
                    $('#js' + i + '2').html(weather);
                    $('#js' + i + '3').html(ob.tempC + '°');
                }
                else {
                    alert('An error occurred: ' + json.error.description);
                }
            }
        });
    });      

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

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