0

我正在整理本教程,http ://www.ezzylearning.com/tutorial.aspx?tid=5869127 。

它完美地工作。我现在要做的是将 aspx 内容托管为 html 文件。这个 html 文件托管在我笔记本电脑上的 wampserver 上。托管在我的测试服务器上的 asp.net 代码。当我尝试访问时,出现以下错误,

Resource interpreted as Script but transferred with MIME type text/html:      "http://201.x.x.x/testAjax/Default.aspx/AddProductToCart?callback=jQuery17103264484549872577_1346923699990&{%20pID:%20%226765%22,%20qty:%20%22100%22,%20lblType:%20%2220%22%20}&_=1346923704482". jquery.min.js:4

Uncaught SyntaxError: Unexpected token <

我不知道如何解决这个问题。

index.html 代码

    $(function () {
        $('#btnAddToCart').click(function () {
            var result = $.ajax({
                type: "POST",
                url: "http://202.161.45.124/testAjax/Default.aspx/AddProductToCart",
                crossDomain: true,
                data: '{ pID: "6765", qty: "100", lblType: "20" }',
                contentType: "application/json; charset=utf-8",
                dataType: "jsonp",
                success: succeeded,
                failure: function (msg) {
                    alert(msg);
                },
                error: function (xhr, err) {
                    alert(err);
                }
            });
        });
    });

    function succeeded(msg) {
        alert(msg.d);
    }

    function btnAddToCart_onclick() {

    }

</script>
</head>
<body>
        <form name="form1" method="post">
    <div>
    <input type="button" id="btnAddToCart"  onclick="return btnAddToCart_onclick()" 
        value="Button" />
</div>
</form>

aspx.vb

Imports System.Web.Services
Imports System.Web.Script.Services

<ScriptService()>
Public Class WebForm1
Inherits Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Session("test") = ""
End Sub

<WebMethod()>
<ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json)>
Public Shared Function AddProductToCart(pID As String, qty As String, lblType As String) As String

    Dim selectedProduct As String = String.Format("+ {0} - {1} - {2}", pID, qty, lblType)

    HttpContext.Current.Session("test") += selectedProduct

    Return HttpContext.Current.Session("test").ToString()

End Function

End Class
4

1 回答 1

0

原因是无论您如何指定 ajax 调用的内容类型,ASP.NET 都会使用 Content-Type text/xml 发送请求

此问题的解决方案位于: http ://bloggingabout.net/blogs/adelkhalil/archive/2009/08/14/cross-domain-jsonp-with-jquery-call-step-by-step-guide.aspx

于 2012-09-06T10:32:27.530 回答