0

我正在执行跨域请求,一旦发出第一个请求,我需要检索 ID(并做一些其他事情),然后将 ID 用于下一个请求。

到目前为止,问题在于下一个请求不喜欢串联。这是我的代码,是的,我知道 document.write 应该更改为 console.log() 但是 console.log 不起作用,我可能会在获得当前问题的帮助后问这个问题。问题从第二个 .ajax() 请求开始。

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script >
$(document).ready(function($) {
 // First link out of three
 var url = 'https://www.sciencebase.gov/catalo
/items?parentId=504108e5e4b07a90c5ec62d4&max=60&offset=0&format=jsonp';

$.ajax({
    type: 'GET',
    url: url,
    jsonpCallback: 'getSBJSON',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(json) {
    for (var i = 36; i < 37; i++) {
        document.write(json.items[i].id);
            var urlId = json.items[i].id;
    }

        var deferUrl = 'https://www.sciencebase.gov/catalog/itemLink/" + urlId 
+"?format=json&max=10';

            $.ajax({
                type: 'GET',
                url: deferUrl,
                jsonpCallback: 'getSBJSON',
                contentType: "application/json",
                dataType: 'jsonp',
                success: function(json) {
                    // Get the next id to use for the next url

                    document.write(deferUrl);

                    $.ajax({
                        type: 'GET',
                        url: 'https://www.sciencebase.gov/catalog
/item/4f4e4b19e4b07f02db6a7f04?format=jsonp',
                        jsonpCallback: 'getSBJSON',
                        contentType: "application/json",
                        dataType: 'jsonp',
                        success: function(json) {
                        document.write(json.title); 
                        },
                        error: function(e) {
                            console.log(e.message);
                        }
                    });
                },
                error: function(e) {
                    console.log(e.message);
                }
            });
    },
    error: function(e) {
        console.log(e.message);
    }
});
});
</script>
</head>
<body>
</body>
</html>

谢谢您的帮助

4

3 回答 3

2
'https://www.sciencebase.gov/catalog/itemLink/" + urlId 
+"?format=json&max=10';

应该

'https://www.sciencebase.gov/catalog/itemLink/' + urlId 
+'?format=json&max=10';

你混合单引号和双引号。使用'srting' + var + 'string'"string" + var + "string"不混合使用

于 2012-11-26T19:14:58.913 回答
0

仅解决您的连接问题..您在那里混合使用单引号和双引号。它们都应该是一致的。

前任:

var deferUrl = 'https://www.sciencebase.gov/catalog/itemLink/' + urlId + '?format=json&max=10';
于 2012-11-26T19:15:58.813 回答
-1

I made a slight change from

for (var i = 36; i < 37; i++) {
            var urlId = json.items[i].id;
    }

 var deferUrl = 'https://www.sciencebase.gov/catalog/itemLink/' + 
 urlId + '?format=jsonp&max=10';

$.ajax({
                type: 'GET',
                url: deferUrl,
                jsonpCallback: 'getSBJSON',
                contentType: 'application/json',
                dataType: 'jsonp',

to

for (var i = 36; i < 37; i++) {
           var urlId = json.items[i].id;
    }

var deferUrl = urlId;

$.ajax({
                type: 'GET',
                url: 'https://www.sciencebase.gov/catalog/itemLink/' + 
                deferUrl + '?format=jsonp&max=10',
                jsonpCallback: 'getSBJSON',
                contentType: 'application/json',
                dataType: 'jsonp',

and that seemed to do the trick. It didn't like having the concatenation done completely before assigning it to the url.

于 2012-11-26T22:38:43.337 回答