George_Fal 2017-06-27 11:13 采纳率: 0%
浏览 118

通过vb.net发布AJAX

I need to do an AJAX post via my vb.net page. This post also attaches the xml file needed to validate the post and then submits it to the link. I dont get any errors when i do the post but the file doesnt get submitted also. I will add my code to this question, thanks in advance.

Public Shared XMLString As String

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim xmldoc As New System.Xml.XmlDocument
    xmldoc.Load("d:/jobfeed.xml")
    XMLString = xmldoc.OuterXml
    ClientScript.RegisterStartupScript(Me.GetType(), "script", POSTtoEP)


End Sub



Public Shared Function POSTtoEP() As [String]
    Dim strurl As String = "https://www.executiveplacements.com/Job_Post_Xml.asp"
    Dim strType As String = "POST"
    Dim sb As New StringBuilder()
    sb.Append("<script type = 'text/javascript'>")
    sb.Append("function POSTtoEP () {")
    sb.Append("$.ajax({")
    sb.Append("type:" & strType & ",")
    sb.Append("url:" & strurl & ",")
    sb.Append("data: { XmlPacket:" & XMLString & ",")
    sb.Append("contentType: application/x-www-form-urlencoded")
    sb.Append("dataType: xml,")
    sb.Append("success: function(data){,")
    sb.Append("},")
    sb.Append("error:function(){")
    sb.Append("alert('Error processing data. Please try again.);")
    sb.Append("}")
    sb.Append("}")
    sb.Append("}")
    sb.Append("});")
    sb.Append("</script>")
    Return sb.ToString()
End Function

End Class

This is the javascript output

  • 写回答

1条回答 默认 最新

  • 谁还没个明天 2017-06-27 12:36
    关注

    There were quotation marks and braces missing in that code. Also the script was declaring a function that was never called.

    Public Shared Function POSTtoEP() As [String]
        Dim strurl As String = "https://www.executiveplacements.com/Job_Post_Xml.asp"
        Dim strType As String = "POST"
        Dim sb As New StringBuilder()
        sb.AppendLine("<script type = 'text/javascript'>")
        sb.AppendLine("function POSTtoEP () {")
        sb.AppendLine("$.ajax({")
        sb.AppendLine("type:""" & strType & """,")
        sb.AppendLine("url:""" & strurl & """,")
        sb.AppendLine("data: { XmlPacket:""" & XMLString & """},")
        sb.AppendLine("contentType:""application/x-www-form-urlencoded"",")
        sb.AppendLine("dataType: ""xml"",")
        sb.AppendLine("success: function(data){ ")
        sb.AppendLine("alert(data);")
        sb.AppendLine("},")
        sb.AppendLine("error: function(error, message){")
        sb.AppendLine("alert('Error processing data. Please try again.');")
        sb.AppendLine("}")
        sb.AppendLine("});")
        sb.AppendLine("};")
        sb.AppendLine("POSTtoEP();")
        sb.AppendLine("</script>")
        Return sb.ToString()
    End Function
    

    This code renders correct javascript. However if Job_Post_Xml.asp doesn't return "application/x-www-form-urlencoded" then ContentType needs to be amended. Otherwise it will produce parse errors. I believe Job_Post_Xml.asp has gzip encoding but can't test it here because it's a cross domain request.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘