问题
I am having trouble getting the VBscript on a .asp page to assign values to variables which are sent by the jQuery ajax() function.
I have this code on several other sites and it works fine, but for some reason it won't work on this website.
Just in case it makes a difference we are running the sites through IIS7.
The issue is that the variables on the VBscript page don't get assigned any of the values passed in the javascript page. However if I change the POST to GET and change request.form to request.queryString it does work. The problem with GET is that we can't enter very much data into the Description field of our form without getting a 414 error - Hence the use of POST.
As I said, I have this code on several other sites so I can't figure out why it isn't working here.
Here is my code (It is a mix of older code and newer code):
javascript:
var prodID = document.forms['hidden'].prodid.value;
var strSubCatID = document.forms['frmProducts'].cmbSubCategory.value;
var strName = encodeURIComponent(document.forms['frmProducts'].txtName.value);
var strDescription = encodeURIComponent(document.forms['frmProducts'].txtDesc.value);
jQuery.ajax({
type: "POST",
url: "/_ajax/products/updateDescription.asp",
data: {
"prodid": prodID,
"strName": strName,
"strSubCatID": strSubCatID,
"strDescription": strDescription
},
success: function () {
alert("Update successful")
},
error: function (xhr) {
alert("Error occured during Ajax request, the error status is: " + xhr.status);
}
});
VBscript:
dim strSubCatID : strSubCatID = request.form("strSubCatID")
dim strName : strName = request.form("strSubCatID")
dim strDescription : strDescription = request.form("strDescription")
dim strProdID : strProdID = request.form("strProdID")
strSQL = "UPDATE tblProducts SET "
strSQL = strSQL & "SubCatID = '" & strSubCatID & "', "
strSQL = strSQL & "Name = '" & strName & "', "
strSQL = strSQL & "Description = '" & strDescription & "' "
strSQL = strSQL & " WHERE ProdID = '" & strProdID & "'"
dim rs : set rs=Server.CreateObject("ADODB.recordset")
rs.Open strSQL, cn, 3, 3
Any help is appreciated.
回答1:
The problem is your variables aren't matching, for example you're POSTing prodid
but looking for strProdID
, also there's no need to encode before passing your data as an object, let jQuery do the encoding internally, for example:
jQuery.ajax({
type: "POST",
url: "/_ajax/products/updateDescription.asp",
data: {
strProdID: $("form[name=hidden] [name=prodid]").val(),
strName: $("form[name=frmProducts] [name=txtName]").val(),
strSubCatID: $("form[name=frmProducts] [name=cmbSubCategory]").val(),
strDescription: $("form[name=frmProducts] [name=txtDesc]").val()
},
success: function() {
alert("Update successful")
},
error: function(xhr){
alert("Error occured during Ajax request, the error status is: " + xhr.status);
}
});
Also on the VBScript side you have:
strName = request.form("strSubCatID")
Which looks like it should be:
strName = request.form("strName")
来源:https://stackoverflow.com/questions/3865269/jquery-ajax-and-vbscript-cant-get-values-in-vbscript-variables-when-using-pos