问候,
点击javascript按钮后,我正在使用jquery发布到网址:
$(".optionClick").click(function () {
var caseOption = $(this).attr('title');
$.post("../tracking/RecordClick.aspx?page=gallery&item=" + caseOption);
});
在被调用的页面上,我使用以下vb.net代码来检索查询字符串变量并将它们写入数据库:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Request.QueryString("page") Is Nothing Then
Dim trackingPage As String = Request.QueryString("page")
Dim trackingIP As String = Request.UserHostAddress
Dim trackingItem As String = Request.QueryString("item")
Dim trackingDate As String = Date.Now().ToString("G")
Try
Dim conString As String
conString = ConfigurationManager.ConnectionStrings("SSFDBConnectionString").ConnectionString
Dim sqlCon As New SqlConnection(conString)
Dim cmd As New SqlCommand
Select Case trackingPage
Case "gallery"
cmd.CommandType = CommandType.Text
cmd.CommandText = "INSERT INTO UserTrackGallery VALUES ('" & trackingIP & "', '" & trackingItem & "', '" & trackingDate & "')"
cmd.Connection = sqlCon
If sqlCon.State = ConnectionState.Closed Then
sqlCon.Open()
End If
cmd.ExecuteNonQuery()
If sqlCon.State = ConnectionState.Open Then
sqlCon.Close()
End If
Case "products"
Case "search"
End Select
Catch ex As Exception
End Try
Response.Close()
End If
End Sub
在本地开发中,这对我来说非常适用于每按一次按钮记录一次数据库写入。但是,在服务器上,每个帖子都会生成1到7个数据库写入。
我一直在寻找解决方案几天无济于事。非常感谢任何帮助!
------------------------------更新---------------- ----------------
我试图通过只用一个按钮创建一个VB.Net页面来简化这个过程:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="javascript.aspx.vb" Inherits="gallery_javascript" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="../Scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajaxSetup({ async: false });
$(".optionClick").click(function () {
$.post("../tracking/RecordClick.aspx?page=gallery&type=click&item=XYZ");
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" class="optionClick" value="button" />
</div>
</form>
</body>
</html>
但问题仍然存在。 Firebug报告“已中止”并且页面被多次调用。我也试过这个javascript:
$(document).ready(function () {
$.ajaxSetup({ url: '../tracking/RecordClick.aspx' });
$(".optionClick").click(function () {
$.ajax({ data: 'page=gallery&type=click&item=XYZ' });
});
});
也试过这个:
url = "../tracking/RecordClick.aspx?page=gallery&type=click&item=XYZ";
if (window.XMLHttpRequest) { // Non-IE browsers
req = new XMLHttpRequest();
try {
req.open("POST", url, false);
} catch (e) {
alert(e);
}
req.send(null);
} else if (window.ActiveXObject) { // IE
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.open("POST", url, false);
req.send();
}
}
上面的每个javascript块按预期在本地工作,但在多个DB写入和Firebug“Aborted”状态下以相同的方式失败。
如果我通过浏览器直接调用url + querystring,一切都按预期工作。
这可能与某种类型的延迟有关,因为它在本地工作吗? 感谢您的帮助!
(编辑)还尝试使用上面代码中的完整路径网址 - 没有任何改进。
答案 0 :(得分:1)
我找到了解决问题的方法。似乎更新版本的.Net会阻止HTTP POST调用,但是localhost允许这样做(这就是它在本地完美运行的原因)。 HTTPWatch和Firebug都显示.aspx页面连接但没有返回响应。似乎多次调用是重试尝试接收响应。
我的解决方案是创建一个Web服务:
Option Strict On
Imports System.Data.SqlClient
Imports System.Data
Imports System
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Web.HttpContext
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://mywebsite.com/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class RecordClick
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function RecordClick() As String
Dim trackingPage As String = ""
Dim trackingIP As String = ""
Dim trackingType As String = ""
Dim trackingItem As String = ""
Dim trackingDate As String = ""
With HttpContext.Current
trackingPage = .Request.Params.Item(0)
trackingIP = .Request.UserHostAddress
trackingType = .Request.Params.Item(1)
trackingItem = .Request.Params.Item(2)
trackingDate = Date.Now().ToString("G")
End With
Dim conString As String
conString = ConfigurationManager.ConnectionStrings("SSFDBConnectionString").ConnectionString
Dim sqlCon As New SqlConnection(conString)
Dim cmd As New SqlCommand
cmd.Connection = sqlCon
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "spInsertRecordClick"
cmd.Parameters.Add("@IPAddress", SqlDbType.NVarChar, 50)
cmd.Parameters("@IPAddress").Direction = ParameterDirection.Input
If trackingIP = "" Then
cmd.Parameters("@IPAddress").Value = DBNull.Value
Else
cmd.Parameters("@IPAddress").Value = trackingIP
End If
cmd.Parameters.Add("@CaseOption", SqlDbType.NVarChar, 50)
cmd.Parameters("@CaseOption").Direction = ParameterDirection.Input
If trackingItem = "" Then
cmd.Parameters("@CaseOption").Value = DBNull.Value
Else
cmd.Parameters("@CaseOption").Value = trackingItem
End If
cmd.Parameters.Add("@TimeDate", SqlDbType.DateTime)
cmd.Parameters("@TimeDate").Direction = ParameterDirection.Input
If trackingDate = "" Then
cmd.Parameters("@TimeDate").Value = DBNull.Value
Else
cmd.Parameters("@TimeDate").Value = trackingDate
End If
cmd.Parameters.Add("@ViewType", SqlDbType.NChar, 10)
cmd.Parameters("@ViewType").Direction = ParameterDirection.Input
If trackingType = "" Then
cmd.Parameters("@ViewType").Value = DBNull.Value
Else
cmd.Parameters("@ViewType").Value = trackingType
End If
cmd.Parameters.Add("@Page", SqlDbType.NVarChar, 50)
cmd.Parameters("@Page").Direction = ParameterDirection.Input
If trackingPage = "" Then
cmd.Parameters("@Page").Value = DBNull.Value
Else
cmd.Parameters("@Page").Value = trackingPage
End If
Try
If sqlCon.State = ConnectionState.Closed Then
sqlCon.Open()
End If
cmd.ExecuteNonQuery()
Catch ex As Exception
Finally
If sqlCon.State = ConnectionState.Open Then
sqlCon.Close()
End If
End Try
If sqlCon.State = ConnectionState.Open Then
sqlCon.Close()
End If
Return "Success"
End Function
End Class
这是我用来调用网络服务的javascript:
$.ajax({
type: "POST",
url: "../tracking/RecordClick.asmx/RecordClick",
data: "page=" + DMOriginatingPage + "&type=" + DMType + "&item=" + DMtitle
});
我必须将以下内容添加到web.config的system.web部分:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
现在效果很好。我在这方面不是很有经验,所以对于我正在尝试做的事情,可能有更好的解决方案。然而,也许这会帮助其他人 - 花了我两个星期来解决这个问题。 :)
答案 1 :(得分:0)
我建议跟踪问题。
1)使用Firefox + Firebug,打开NET选项卡并观察发送的请求数量。
2)将您的AJAX URL粘贴到浏览器的地址栏中,看看服务器上发生了什么。这样您就可以确定只有一个请求。
答案 2 :(得分:0)
您是否尝试过使用帖子中网站根目录的完整路径...而不是使用“../"?
怀疑代码:
$(".optionClick").click(function () {
$.post("../tracking/RecordClick.aspx?page=gallery&type=click&item=XYZ");
});
尝试删除“..”并将其替换为实际路径...我怀疑这可能会导致您的问题。