0

I have a .html page which has a reference to a remote javascript file Data.js. In Data.js there is a function which calls navigator.appName and assigns the generated value to a HiddenField already defined in my .html page.

Page.html

  <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript" src="http://server.com/Data.js" ></script> 
    </head>
    <body style = "font-family:Arial; font-size:10pt" onload="Execution();">
    <form id="form1">
    <div>
        <input type="hidden" id="Hidden2" />
        </div>
        </form>
    </body>
    </html>

Data.js

function Myfunction()
{
var val = navigator.appName;
document.getElementByID('Hidden2').value = val
}

I also had another function in place to post Hidden2 value to a remote server in order to store it in a database such as follows:

    function POSTHidden() {
        $.ajax({
            type: "POST",
            url: "http://server.com/VB.aspx/GETHidden",
            data: '{name: "' + $("#Hidden2")[0].value + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }
function OnSuccess(response) {
    alert(response.d);
}

And a third function which executes the first function MyFunction(); then the second function POSTHidden(); as follows:

function Execution()
{
myFunction();
POSTHidden();
}

Now on my VB.aspx there is a WebMethod called GETHidden which basically passes a string as an argument and store it in the database such as follows:

<System.Web.Services.WebMethod()> _
Public Shared Function GETHidden(ByVal value As String) As String
Dim value = String.Empty
Dim str = value.Split(New Char() {" "c})
Dim conn As String

conn = ConfigurationManager.ConnectionStrings("connStr").ConnectionString
Dim sqlcon As New SqlConnection(conn)

Try
sqlcon.Open()
Dim cmd = New SqlCommand("Insert into table (Record1) values (@param)", sqlcon)
cmd.Parameters.AddWithValue("@param", value)
cmd.ExecuteNonQuery()
cmd.Dispose()
sqlcon.Close()

Catch ex As Exception

End Try
End Function

All of the above works ONLY IF the .html page is in the same domain as the ajax request is generated from or the VB.aspx is located according to this post. As I have tried moving the .html out of the domain I usually upload to and upload it onto another domain, when opening the .html, it doesn't work whereas it works if it is in the same domain name. What I am trying to achieve is that to have a .html file located anywhere and when running, the data collected from the referenced Data.js should be posted to the remote server and stored in the database. Therefore, the .html page should work from anywhere remotely (despite the domain it is in or whether on the local PC) and the data should be passed or posted to the remote server (via an ajax request or any POST request that could solve the problem) so that Hidden2 can be stored in the database. Bearing in mind, browser compatibility and multiple-users opening the page at the same time (multi-post requests to server). Any suggestions or ideas would be much appreciated.

Community
  • 1
  • 1
HShbib
  • 1,811
  • 3
  • 25
  • 47

1 Answers1

0

You have to specify access control header in the server side to process cross domain requests

Access-Control-Allow-Headers

HTTP access control (CORS)

Refer these. Its a little bit tricky in .net comparing to other

http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

http://patelshailesh.com/index.php/cross-domain-asp-net-web-api-call-using-ajax

SarathSprakash
  • 4,614
  • 2
  • 19
  • 35