0

我不是专业的程序员,并试图从网上获取这个,如果我的问题看起来很愚蠢,请提前抱歉,但我过去 2 天一直在研究这个无济于事。

这是我的代码:

function view(data)
{
    alert (data);
}

function test()
{
    var url = 'someurl';

    $.ajax({
        type: 'POST',
        url: url,
        async: false,
        contentType: "application/json",
        dataType: 'jsonp',
        data: "Basic=YWRtaW46cHNhZG0xbg==",
        headers: {
            "Basic": 'YWRtaW46cHNhZG0xbg=='
        },
        success: view(data)
    })
}

但是每当我运行它时,我都会收到一条错误消息:“数据未定义”。我如何访问请求得到的实际响应?我相信,如果你们能给我一个提醒响应的代码(但使用我的代码),我可以从那里获取它。

谢谢!!!


所以伙计们,首先非常感谢您快速而详细的回复!

我尝试了这两种方法,我没有收到“未定义数据”的错误,我想我不明白的是,我在调用什么函数?当我只想将“数据”放入变量中时,我需要一个代理函数怎么办?

话虽如此,我遇到了一个新问题——由于某种原因,FF/Chrome 忽略了我设置的属性,并在没有标题的情况下发送它,并且作为“GET”发送,这会导致错误:

Request .com:xxxx?callback=jQuery191023387945420108736_1384464320088&        Basic=YWRtaW46cHNhZG0xbg==&_=1384464320089
Request Method:GET
Status Code:401 Unauthorized
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Cookie:JSESSIONID=EC56CA6ADB540E1B6785B318DD0886CD
Host:IP:8083
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/30.0.1599.101 Safari/537.36
Query String Parametersview sourceview encoded
callback:jQuery191023387945420108736_1384464320088
Basic:YWRtaW46cHNhZG0xbg
_:1384464320089
Response Headersview source
Content-Length:77
Date:Wed, 13 Nov 2013 11:15:40 GMT
Secsph-Request-Id:1164775931060804669
Server:NA"

主要请求还伴随着次要请求,我不确定是什么调用了它,或者它是否与问题有关:

Request :nikkomsgchannel        /e?00160023002b00550046004b00660050005e005800280055005c007a002200590050004d004a005600520004002000530055003600210010005d005900540056000b006a003300500054002c0030005400470056001f0047004f00490023000f005b003000300042005c0056001f00550059004b002b000f0057002c002f005d005c00560050005f005a
Request Headersview source
Cache-Control:no-cache
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/30.0.1599.101 Safari/537.36
Query String Parametersview sourcevie encoded
00160023002b00550046004b00660050005e005800280055005c007a002200590050004d004a005600520004002000530055003600210010005d0

有什么想法吗?

4

3 回答 3

0

试试这个..

function test() {
            var url = 'someurl';
            $.ajax({
                type: 'POST',
                url: url,
                async: false,
                contentType: "application/json",
                dataType: 'jsonp',
                data: "Basic=YWRtaW46cHNhZG0xbg==",
                headers: {
                    "Basic": 'YWRtaW46cHNhZG0xbg=='
                },
                success: function(data) {
                     console.log(data);
                     // /\ this will show on console the returned data.
                     //the variable 'data' actually contains the returned data.
                     // here you can do whatever you want with the returned data
                     //if you want to alert the response:
                     alert(data);
                }
           });
        }
于 2013-11-14T21:14:49.463 回答
0

尝试:

function test() {
    var url = 'someurl';

    $.ajax({
        type: 'POST',
        url: url,
        async: false,
        contentType: "application/json",
        dataType: 'jsonp',
        data: "Basic=YWRtaW46cHNhZG0xbg==",
        headers: {
            Basic: 'YWRtaW46cHNhZG0xbg=='
        },
        success: function (data) {
            alert(JSON.stringify(data));
        }
    });
}

这似乎是你想要做的(警告 JSON 字符串)

于 2013-11-14T21:18:19.517 回答
0

当您这样做时,立即success: view(data)执行view(data)(尝试将data当前不存在的函数传递给view函数),并尝试将该函数的返回值设置为success回调。

相反,您需要做的只是将函数引用作为回调传递,因此:

success: view

当 AJAX 请求返回成功响应时,该view函数将被调用,并将响应作为参数传递给data参数。

于 2013-11-14T21:51:33.627 回答