使用ajax 400错误请求错误

时间:2012-12-04 05:55:41

标签: ajax web-services

我使用ajax来调用web服务。但是当我检查firebug中的输出时,我得到400错误的请求错误。你能告诉我为什么我会收到错误。为什么我会收到错误。下面的代码有什么问题。以下是我写的代码:

<script>

    function HelloWorld() {

        var sDate = '<cart currency="USD" total="5.38"><cart-shipment total="5.38" shipment-reference="33261668"><shipment-tax total="0.39" /><shipment-cost total="0" /><shipment-address><address>33 W 59TH STREET APT 203 </address><zip>60559</zip><city>WESTMONT</city><state>IL</state><country>US</country><phone></phone><name>John Baker</name><attention-of>KID</attention-of></shipment-address><items><item total="4.99"><item-name>Hello Kitty - cupcake</item-name><item-description>Hello Kitty - cupcake</item-description><item-price>4.99</item-price><item-quantity>1</item-quantity></item></items></cart-shipment></cart>';


        var webMethod = "https://abc.com/Services/TransactionService.svc";

        var sr = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:ns2="vp">' +
               '<soapenv:Header/>' +
                    '<soapenv:Body>' +
                        '<tem:ProcessTransaction>' +
                            '<tem:checkOutData>' + sDate + '</tem:checkOutData>' +
                            '<tem:token>5OMc0y1miZN5EFqiZ8IiLn+mzdboyTuTob43Kp4+VcVtrYGQvl7QWJB5OeoPWQpBUej6LSejwE8f16tDhg1EUqDtGGAdn/MM3Gk8MOr0FvFko84ogfhIs9HCUjum2MUN1a/sALjhen+DareUP5wWbIpnu8Eaqg2Tv0RjEsq1bqYblHcXfKIq7anTDzYoHN8Y7LAXgdEhSrVcEIB3+sCCDQ==</tem:token>' +
                            '<tem:transactionDescription>Volusion Order Description</tem:transactionDescription>' +
                        '</tem:ProcessTransaction>' +
                    '</soapenv:Body>' +
                '</soapenv:Envelope>';


        $.ajax({
            type: "POST",
            url: webMethod,
            data: sr,
            contentType: "text/xml",
            dataType: "xml",
            cache: false,
            success: OnSuccess,
            error: OnError
        });
        return false;

    }
    function OnSuccess(data, status) {

        alert(data);

    }

    function OnError(request, status, error) {

        alert(error);
    }


    </script>
    <div>
            <input type="button" value="Soap" onclick="HelloWorld();" />
        </div>

以下是我在firebug中收到的输出:

请求标题: 选项/Services/TransactionService.svc?wsdl HTTP / 1.1 主持人:development.virtualpiggy.com 连接:保持活力 访问控制请求方法:POST 来源:http://test.com 用户代理:Mozilla / 5.0(Windows NT 6.0)AppleWebKit / 537.11(KHTML,与Gecko一样)Chrome / 23.0.1271.95 Safari / 537.11 Access-Control-Request-Headers:origin,content-type,accept 接受: / 推荐人:http://test.com/Desktop/test_webservice.htm Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en; q = 0.8 Accept-Charset:ISO-8859-1,utf-8; q = 0.7,*; q = 0.3

响应标题: Access-Control-Allow-Origin:http:// yann-asus:81 http://www.kkdmarketing.com 缓存控制:私人 内容长度:0 日期:2012年12月4日星期二06:42:23 GMT 服务器:Microsoft-IIS / 7.0 设置Cookie:会话ID = ovpsrxt33jyoi3pzidvxkh0o;路径= /;安全;仅Http X-ASPNET-版本:4.0.30319 X供电-通过:ASP.NET

1 个答案:

答案 0 :(得分:0)

我会尝试在contentType

中设置utf-8字符集
$.ajax({
        type: "POST",
        url: webMethod,
        data: sr,
        contentType: "text/xml; charset=utf-8",
        dataType: "xml",
        cache: false,
        success: OnSuccess,
        error: OnError
});