2

我正在尝试使用获取请求返回一系列产品。响应返回带有 200 请求的 XML。

网络服务:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public List<product> GetAllProducts()
{
    using (SchulteDesignYourOwnEntities db = new SchulteDesignYourOwnEntities())
    {
        return db.products.ToList();
    }
}

这是我的代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title></title>
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {

                $.ajax({
                    url: 'http://www.organizeliving.com/designwebservice.asmx/GetAllProducts',
                    dataType: 'json',
                    success: function (result) {
                        alert("Result: " + result.length);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        console.log("Status: " + xhr.status);
                        console.log("Message: " + thrownError);
                    }
                });


            });
        </script>
    </head>

    <body></body>

</html>
4

1 回答 1

15

你有dataTypeas 'json'。jQuery 会自动尝试从响应中解析 JSON。如果不能,则认为这是一个错误。

XML 不是有效的 JSON(它真的很讨厌开头<)。您可以将其更改dataType'xml'(或不更改)或实际从服务器发出纯 JSON。

于 2012-12-05T15:32:25.933 回答