ASP.NET Web服务希望ajax、json、jquery和vb.NET

ASP.NET Web服务希望ajax、json、jquery和vb.NET,asp.net,ajax,vb.net,json,Asp.net,Ajax,Vb.net,Json,我正在整理这个教程 它工作得很好。我现在要做的是将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?call

我正在整理这个教程

它工作得很好。我现在要做的是将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 <
资源解释为脚本,但使用MIME类型text/html传输:http://201.x.x.x/testAjax/Default.aspx/AddProductToCart?callback=jQuery17103264484549872577_1346923699990&{%20pID:%20%226765%22、%20qty:%20%22100%22、%20LBL类型:%20%2220%22%20}&&=1346923704482”。jquery.min.js:4
未捕获的语法错误:意外标记<
我不知道如何解决这个问题

index.html代码

$(函数(){
$('#btnAddToCart')。单击(函数(){
var result=$.ajax({
类型:“POST”,
url:“http://202.161.45.124/testAjax/Default.aspx/AddProductToCart",
跨域:是的,
数据:{pID:“6765”,数量:“100”,LBL类型:“20”},
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“jsonp”,
成功:成功,,
故障:功能(msg){
警报(msg);
},
错误:函数(xhr,err){
警惕(err);
}
});
});
});
函数成功(msg){
警报(msg.d);
}
函数btnAddToCart_onclick(){
}

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
导入System.Web.Services
导入System.Web.Script.Services
公共类WebForm1
继承页面
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
会话(“测试”)=“”
端接头
公共共享函数AddProductToCart(pID为字符串,qty为字符串,lblType为字符串)为字符串
Dim selectedProduct As String=String.Format(“+{0}-{1}-{2}”,pID,qty,lblType)
HttpContext.Current.Session(“测试”)+=selectedProduct
返回HttpContext.Current.Session(“test”).ToString()
端函数
末级

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

有关此问题的解决方案,请访问:

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