从JavaScript调用webservice - 错误500

时间:2014-02-03 13:43:41

标签: javascript web-services

我想打电话给第三方网络服务。当我使用Postman(插件到Google Chrome)时,一切正常,但是当我从JavaScript执行此操作时,它无效。我收到内部服务器错误(500)。为什么?我做错了什么?

这是Postman的HTTP请求预览:

POST /ws/login.php HTTP/1.1
Host: mobile.example.com
Content-Type: text/xml; charset=utf-8
SOAPAction: http://tempuri.org/login
Cache-Control: no-cache
Postman-Token: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

<?xml version="1.0" encoding="utf-8"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <getUserInfo_server xmlns:="http://tempuri.org"> <__numeric_0> <login xsi:type="xsd:string">username</login> <pwd xsi:type="xsd:string">password</pwd> </__numeric_0> </getUserInfo_server> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

这是JS的代码形式:

var requestBody =
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">' +
        '<SOAP-ENV:Body>' +
            '<getUserInfo_server xmlns:="http://tempuri.org">' +
                '<__numeric_0>' +
                    '<login xsi:type="xsd:string">username</login>' +
                    '<pwd xsi:type="xsd:string">password</pwd>' +
                '</__numeric_0>' +
            '</getUserInfo_server>' +
        '</SOAP-ENV:Body>' +
    '</SOAP-ENV:Envelope>';

var request = new XMLHttpRequest();
request.open('POST', 'http://mobile.example.com/ws/login.php', true);
request.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
request.setRequestHeader('SOAPAction', 'http://tempuri.org/login');
request.send(requestBody);

我正在尝试使用$ .post,$ .soap,$ .ajax。我安装插件以防止CORS(Allow-Control-Allow-Origin: *)。

1 个答案:

答案 0 :(得分:0)

通常500个错误是后端的问题,而不是前端的问题。因此,您的请求可能非常好,但后端崩溃了。如果您可以检查服务器中的日志,则可能知道确切的问题。