jquery - 出现网络错误 415 - 不支持的媒体类型

标签 jquery xml ajax soap

在我的 HTML5 应用程序中,当我尝试调用 SOAP Web 服务时,出现“不支持的媒体类型”错误。

这是我的 JavaScript 函数的代码。

function login()
{
    var soapMessage = '<?xml version="1.0" encoding="UTF-8"?>'+
    '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:blu="http://www.bluedoortech.com/">'+
    '<soapenv:Header/>'+
    '<soapenv:Body>'+
        '<blu:Connect>'+
            '<blu:userID>' +  $("#txtUserName").val() + '</blu:userID>'+
            '<blu:pwd>' + $("#txtPassword").val() + '</blu:pwd>'+
        '</blu:Connect>'+
    '</soapenv:Body>'+
    '</soapenv:Envelope>';


    $.ajax({
        url : 'Wealth.asmx' ,
        data: soapMessage,
        type: "POST",
        dataType: "xml", 
        cache : false,
        processData: false
    }).success(function(xmlDoc,textStatus) {
        alert($(xmlDoc).text());
    });
}[1]

这里我还附上了错误的屏幕。

出于测试目的,我创建了一个 php 文件,并使用该 php 文件来调用此 SOAP Web 服务。当我连接到网络服务时它运行得很好。这是 PHP 代码。

        header("Content-type: text/xml"); 
        $soap_request = file_get_contents('php://input');

        $xml = simplexml_load_string($soap_request);

        $userIDTag = $xml->xpath('//blu:userID');
        $userID = $userIDTag[0][0];

        $passwordIDTag = $xml->xpath('//blu:pwd');
        $password = $passwordIDTag[0][0];

        $client = new SoapClient("Wealth.asmx?WSDL", array('trace' => true));
        $objLogin =  $client->Connect(array('userID'=>$userID,'pwd'=>$password));

        echo $client->__getLastResponse();

请帮助我确定问题。

最佳答案

正如 Joachim Isaksson 所建议的,我已经添加了内容类型 header ,现在运行良好。我也将其发布在这里。

function login()
{
    var soapMessage = '<?xml version="1.0" encoding="UTF-8"?>'+
    '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:blu="http://www.bluedoortech.com/">'+
    '<soapenv:Header/>'+
    '<soapenv:Body>'+
        '<blu:Connect>'+
            '<blu:userID>' +  $("#txtUserName").val() + '</blu:userID>'+
            '<blu:pwd>' + $("#txtPassword").val() + '</blu:pwd>'+
        '</blu:Connect>'+
    '</soapenv:Body>'+
    '</soapenv:Envelope>';


    $.ajax({
        url : 'Wealth.asmx' ,
        data: soapMessage,
        headers: {
            "Content-Type":"text/xml"
        },
        type: "POST",
        dataType: "xml", 
        cache : false,
        processData: false
    }).success(function(xmlDoc,textStatus) {
        alert($(xmlDoc).text());
    });
}

关于jquery - 出现网络错误 415 - 不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15518005/

相关文章:

javascript原型(prototype)类,这在jquery click中

jquery - 如何在移动 Safari 中短暂禁用滚动?

ios - XML 解析数据没有从 viewController 获取到 objective-c 中的 tableViewController?

javascript - 使用 XML 解析器从用户获取最近的 YouTube 视频

javascript - 根据其他下拉菜单更改下拉菜单

javascript - 使用 History_API 创建深层链接

javascript - Jquery 将前一个同级的第三个子级中的标签作为目标

jquery - jstree选择节点

c++ - QDom 递归删除评论

javascript - Axios 不会发送 cookie,Ajax (xhrFields) 就可以了