0

I have this jQuery code that runs from a form submission and loops to insert a certain number of records into an SQL table. The ajax portion of the code calls a VB function in a webmatrix page called Helpers.vbhtml

function InsertSheeter()
{
    var textDate = $('#textDate').val()
    var Workorder = $('#textWorkorder').val()
    var Material = $('#dropdownMaterial').val()
    var Shift = $('#dropdownShift').val()
    var Sheeter = $('#dropdownSheeter').val()
    var FoilNum1 = $('#textFoilNum1').val()
    var FoilNum2 = $('#textFoilNum2').val()
    var FoilNum3 = $('#textFoilNum3').val()
    var Printline = $('#dropdownPrintline').val()
    var Section = $('#dropdownSection').val()
    var Comments = $('#textComments').val()
    var Employee = $('#dropdownEmployees').val()

    var a = 0

    while (a < Section)
    {


        switch (a) 
        {
        case 0:
            blockSection = "1"
            break;
        case 1:
            blockSection = "2"
            break;
        case 2:
            blockSection = "3"
            break;
        }

    var str = {pDate: textDate, pSheeter: Sheeter, pShift: Shift, 
            pEmployee: Employee, pWorkorder: Workorder, pBlockSection: blockSection, 
                pComments: Comments, pFoilNum1: FoilNum1, pFoilNum2: FoilNum2, 
                pFoilNum3: FoilNum3, pPrintline: Printline, pMaterial: Material}
    $.ajax(
    {
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Helpers.vbhtml/InsertSheeter",
        data: JSON.stringify(str),    
        dataType: "json",
        success: function(data)
            {
            OpenReports(Workorder, data.d)
            },
        error: function (xhr, errorType, exception) { 
                var errorMessage = exception || xhr.statusText;
                alert(errorMessage);
                }

    });


    a++;
    }


}

This is the VB.NET function that gets called:

I created according to how I understand the documentation, a webservice using a vbhtml page in the App_Code directory. Here is a snippet of my vb code InsertSheeter as previously referenced in this post.

@Imports Microsoft.VisualBasic
@Imports System
@Imports System.Web
@Imports System.Web.Services
@Imports System.Xml.Serialization

@Functions
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> 


 <WebMethod>
Public Shared Function InsertSheeter(ByVal pDate As DateTime, ByVal pSheeter As String, ByVal pShift As String,
                            ByVal pEmployee As String, ByVal pWorkorder As String,
                            ByVal pBlockSection As String, ByVal pComments As String, ByVal pFoilNum1 As String,
                            ByVal pFoilNum2 As String, ByVal pFoilNum3 As String, ByVal pPrintline As String,
                            ByVal pMaterial As String) As String

Dim db As Database
db = Database.Open("Nomex")
Dim strQuery As String

Dim pBlockNumber As String

pBlockNumber = GetBlockNumber(pBlockSection, pWorkorder, pSheeter, pMaterial)

strQuery = "INSERT INTO dbo.NomexSheeter (SheeterDate, Sheeter, Shift, Employee, WorkOrder, BlockNumber, BlockSection, " _
            & "Comments, FoilNum_1, FoilNum_2, FoilNum_3, PrintLine, Material) " _
            & "VALUES (@0, @1, @2, @3, @4, " _
            & "@5, @6, @7, @8, @9, @10, @11, @12)"

db.Execute(strQuery, pDate, pSheeter, pShift, pEmployee, pWorkorder, pBlockNumber, pBlockSection,
                     pComments, pFoilNum1, pFoilNum2, pFoilNum3, pPrintline, pMaterial)

db.Close

Return pBlockNumber 

End Function


End Functions

after calling the jQuery function InsertSheeter() i still get the 404 Forbidden error. I don't understand what I am doing wrong. Webmatrix documentation and posts online seem to support this idea.

Ryan
  • 650
  • 3
  • 15
  • 49
  • 1
    it's definitely not that the two functions are named the same. What do you mean that it worked until you created the function in jQuery? What was the previous code that worked? – admanb Mar 06 '14 at 00:55
  • Javascript client functions have no relationship to server-side code, so forget the naming. Suggest you run fiddler2 (http://www.telerik.com/fiddler) and see what traffic is being passed and what the response it. There are too many other things that could cause a mismatch (wrong relative paths for the api call etc). – iCollect.it Ltd Mar 06 '14 at 08:58
  • Thanks TrueBlueAussie and admanb. Does the code look fundamentally ok? Am I trying to do the right thing? I will report back after running fiddler. – Ryan Mar 06 '14 at 23:56
  • THis is what I get. Description: The type of page you have requested is not served because it has been explicitly forbidden.     Please review the URL below and make sure that it is spelled correctly.

    Requested URL: /Helpers.vbhtml/ValidateWorkorder


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1022
    – Ryan Mar 07 '14 at 18:13
  • Hi - Are you running this in iis on a server or through VS built in web server ? – Symeon Breen Mar 14 '14 at 15:15
  • Sometimes security issues may arise at the browser sandbox as they go outside of the loaded domain. So, make sure if your server resources are in the same domain. I've faced issues even my using `localhost vs IP address.` – Chand Priyankara Mar 17 '14 at 12:29

2 Answers2

1

Perhaps your AJAX request is going over HTTP and you have "Require SSL" set for your application?

mikey
  • 5,090
  • 3
  • 24
  • 27
  • I don't see SSL as being set in the site settings. Does the code look fundamentally right? – Ryan Mar 10 '14 at 15:17
  • Why do you have the code in two places? What are the file names and paths relative to the web site root for each location? – mikey Mar 11 '14 at 00:26
  • "I created according to how I understand the documentation" <-- can you point me to said documentation? – mikey Mar 11 '14 at 00:30
  • I removed some duplicate code in my post. I did not mean formal documentation. I meant reading various posts and such online. I also thought that you could create web methods that could be called from jQuery. The calling jQuery is in the root, and the vb.net code is in App_Code – Ryan Mar 11 '14 at 00:49
  • A couple of things to consider, App_Code is a protected directory so you cannot request files in there. That said, it doesn't look like you're requesting a file from in there, instead you are requesting this: Helpers.vbhtml/InsertSheeter -- how did you determine this URL? It looks a bit like an MVC action (and I don't think you're supposed to be using webmethods with MVC). This link might help troubleshoot. http://www.asp.net/web-pages/overview/more-resources/aspnet-web-pages-(razor)-troubleshooting-guide – mikey Mar 11 '14 at 04:44
1

Maybe I'm wrong but I think Helpers is not intended to be called by client side.

Helpers In ASP.NET Web Pages are components that are stored as .cshtml files, and are meant to promote reusability by allowing other ASP.NET Web Pages to access them using the familiar object.method([args…]) syntax in views.

Usefull link about helpers.

What you seems to want is a web service in asmx format that allow http Post request and send and receive JSON.

Link to create web services. Link to enable http Post. Link to enable JSON .

Community
  • 1
  • 1
jlvaquero
  • 8,571
  • 1
  • 29
  • 45