jQuery AJAX and VBscript - Can't get values in VBscript variables when using POST in the jQuery AJAX

给你一囗甜甜゛ 提交于 2020-01-03 04:56:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!