“Uncaught SyntaxError:Unexpected token<"”成功的HTTP请求之后 [英] "Uncaught SyntaxError: Unexpected token <" after successful HTTP Request

查看:958
本文介绍了“Uncaught SyntaxError:Unexpected token<"”成功的HTTP请求之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有SAP后端系统提供的ODATA REST服务的jQuery进行AJAX。

I am trying to do AJAX using jQuery with an ODATA REST service provided by an SAP backend system.

          $.ajax({
                type: 'GET',
                url: 'http://sapxx.sapms/sap/opu/odata/.../',                   
                async: true,
                dataType: 'jsonp',                  
                username: 'username',
                password: 'password',
                crossDomain: true,                  
                success: function() {
                    alert('Done.');
                },
                error: function() {
                    alert('Error.');
                },
            });

请求返回状态200,我从服务器得到响应,称为?callback = jQuery3100687 ...并包含xml代码。所有这些都在Chrome调试器中可见。但是在成功的HTTP请求之后我得到了上述错误

The request returns status 200 and I get a response from the server which is called something like "?callback=jQuery3100687..." and contains xml code. All this is visible in the Chrome debugger. But after the successful HTTP Request I get the aforementioned error

Uncaught SyntaxError: Unexpected token <

我怀疑错误是由于dataType参数引起的,该参数设置为jsonp请求。有没有办法解决这个错误?服务器只能使用XML格式进行响应。该请求仅在dataType设置为jsonp时有效,我猜是因为它启用了CORS。发送请求后,我收到错误警告,尽管有200状态。

I suspect the error is due to the "dataType" parameter which is set to "jsonp" in the request. Is there any way to work around this error? The server can only respond using XML format. The request only works when the dataType is set to "jsonp", I guess because it enables CORS. After sending the request, I get the "Error" alert despite the 200 status.

推荐答案

你说你得到了 xml 来自服务器的响应。你的ajax请求是为jsonp设置的,但是:

You say you get an xml response from the server. Your ajax request is set up for jsonp, though:

dataType: 'jsonp',

请改为尝试:

dataType: 'xml',
success: function(xml) {
    //remainder of the code
}

如果将 dataType 设置为 jsonp ,jQuery将尝试解析响应作为JSON。这会导致错误,因为响应不是JSON。

If you set the dataType to jsonp, jQuery will try to parse the response as JSON. This results in errors, because the response ins't JSON.

这篇关于“Uncaught SyntaxError:Unexpected token&lt;&quot;”成功的HTTP请求之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆