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
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.