weixin_33743703 2014-05-14 08:32 采纳率: 0%
浏览 31

Ajax发布后页面重新加载

I am doing a Single Page Application using asp.net MVC

When I'm posting to the server from Ajax, The Web API validates the parameters sent, then returns a Json String. Here's the function in my Web API

Public Class VerifyReturn
    Public SerBool As String
    Public SerMessage As String
    Public TextBool As String
    Public TextMessage As String
End Class

<HttpPost>
Public Function ValidateParam(ByVal ser As Integer, ByVal content As String) As List(Of VerifyReturn)
    Dim ErrorsJson As String = ""
    Dim Errors As Boolean = False


    Try
        If Not ser = Nothing AndAlso IsNumeric(ser) = True Then  
            ErrorsJson &= "[{""SerBool"":""0"",""SerMessage"":"""","
        Else
            ErrorsJson &= "[{""SerBool"":""1"",""SerMessage"":""Not Valid."","
            Errors = True
        End If

       If Not content Is Nothing AndAlso content.Length < 151 Then
            ErrorsJson &= """TextBool"":""0"",""TextMessage"":""""}]"
       Else
            ErrorsJson &= """TextBool"":""1"",""TextMessage"":""Content Too Large.""}]"
            Errors = True
        End If

        Dim ReturnJson = JsonConvert.DeserializeObject(Of List(Of VerifyReturn))(ErrorsJson)
        Return ReturnJson


    Catch ex As Exception

    End Try
End Function

This is my Ajax:

 $(document).ready(function () {
      $('#BtnInsert').click(function () {

           $.ajax({
                    url: '@Url.Content("~/api/ContentM/ValidateParam?ser=")' + $('#SerList').val() + ("&content=") + $('#TxtContent').val(),
                    type: 'POST',
                    success: function (responseData) {
                        for (var i = 0; i < responseData.length; i++) {

                            If(responseData[i].TextBool == '1')
                            {
                                $('#LblErrors').val(responseData[i].TextMessage);
                            }
                        }
                    }
                });
          });
      });

The API function is being triggered and the procedure server side is going fine,

But when the ajax call finishes, the page reloads and I see nothing in my $('#LblErrors') Label.

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • weixin_33681778 2014-05-14 13:51
    关注

    Try this:

    $(document).ready(function () {
      $('#BtnInsert').click(function (e) {
           e.preventDefault();
    
           $.ajax({
                    url: '@Url.Content("~/api/ContentM/ValidateParam?ser=")' + $('#SerList').val() + ("&content=") + $('#TxtContent').val(),
                    type: 'POST',
                    success: function (responseData) {
                        for (var i = 0; i < responseData.length; i++) {
    
                            If(responseData[i].TextBool == '1')
                            {
                                $('#LblErrors').val(responseData[i].TextMessage);
                            }
                        }
                    }
                });
          });
      });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64