Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在客户端函数(Javascript)中调用服务器端方法_Javascript_Asp.net_Client_Client Side_Serverside Javascript - Fatal编程技术网

在客户端函数(Javascript)中调用服务器端方法

在客户端函数(Javascript)中调用服务器端方法,javascript,asp.net,client,client-side,serverside-javascript,Javascript,Asp.net,Client,Client Side,Serverside Javascript,有人能帮我在javascript函数中调用VB.NET方法吗?我的方法不是共享/静态的,不返回任何内容。它只是简单地将数据保存到数据库并重定向用户。请帮帮我,这是我的密码: VB方法 Public Function SaveVehicles() Dim res As Boolean If res = True Then Dim sModel As String = cboModel.SelectedValue

有人能帮我在javascript函数中调用VB.NET方法吗?我的方法不是共享/静态的,不返回任何内容。它只是简单地将数据保存到数据库并重定向用户。请帮帮我,这是我的密码:

VB方法

  Public Function SaveVehicles()
          Dim res As Boolean
             If res = True Then
            Dim sModel As String = cboModel.SelectedValue
            Dim sVariant As String = cboVariant.SelectedValue
            Dim sColor As String = cboColor.SelectedValue

            cboModel.SelectedValue = sModel
            cboVariant.SelectedValue = sVariant
            cboColor.SelectedValue = sColor


            Dim oData As New WebServVehSwapping
            Dim strSql As String
            Dim sDealer As String
            Dim sUserName As String

            'sDealer = "TBP01"
            sDealer = Trim(Request.QueryString("dealercode"))
            If sDealer = "" Then sDealer = "TBP01"
            sUserName = "User1"

            '------------------------------------------
            strSql = oData.InsertTblRequirement( _
              sDealer _
             , Now.ToString _
             , sUserName _
             , cboColor.Text.Trim _
             , cboModel.Text.Trim _
             , cboVariant.Text.Trim, "Open")
            MsgBox("OKAY")
            Response.Redirect("MyRequirements.aspx?DealerCode=" & sDealer)
        Else
            'do Nothing
        End If
    End Function
这是我的Javascript函数

   function ConfirmView()
    {   
        var Ok = confirm('There is/are existing vehicle(s) in Network Vehiches for sale, View Vehicle(s)?');
        if(Ok==true)
        {

       location.href = 'NetworkVehiclesforSale.aspx';
        return false;
        }
        else if (Ok!=true)
        {

         //THE VB METHOD GOES HERE     
      }
}
我尝试过回调处理程序,它只适用于返回某物/字符串的函数

我尝试过Pagemethod,但它只适用于静态/共享函数。请帮帮我,我真的很需要它。请。谢谢

你可能想读“构建Windows通信基础服务介绍”-

特别是:“使用WCF 3.5设计和构建RESTful Web服务指南”——

并查看一些javascript库,它们使调用RESTful web服务变得简单,比如jQuery-

使用jQuery,您可以调用服务器端VB.NET代码,如下所示:

$.ajax({  
    type: "GET",  
    contentType: 'text/json',  
    dataType: 'json',  
    url: "https://URL-TO-SERVICE-HERE/...",  
    timeout: 10000,  
    error: function () {  

        // Deal with the error  

    },  
    success: function (data) {  

        // Do something with the result
        // Example:
        if (data.dealerCode) 
            location.href = 'MyRequirements.aspx?DealerCode=' + data.dealerCode;


    }  
});  

.Net web服务无法发挥神奇的作用,也就是说,您不能对服务器上的Ajax请求发出重定向响应,而期望整个页面被重定向。
唯一会发生的事情是Ajax调用被重定向到另一个页面,并尝试从那里获取数据。如果您想在客户端浏览器中更改页面,必须通过JavaScript在客户端执行,例如,
document.location=url\u由您的函数返回\u通过\u ajax\u调用

,在这段代码的哪一部分我可以调用savevevehicles()方法?您可以将这个JavaScript片段放在您编写的地方”//VB方法放在这里“在你的问题中。您可以在代码段中更改“URL到服务”,以指向您的.svc文件(WCF服务),它将运行您的“SaveVeCICE()”方法。我如何调用SaveVeCeleSe()方法?如果我的答案对您有帮助,如果您将它标记为答案,或问后续问题,我将非常感激。