I have a case where i want to submit a form and get the response from this submit, because based on this response "at least submit complete response" i will make some other logic.
What makes my case is different that this form is on a domain and i am submitting it to another domain name so can't use ajax submit due to cross script security issues.
Anyone face a case like this and have a solution?
I used JQuery code to submit the form:
$('#MyForm').submit();
Form code:
<form target="MyIframe" name="MyForm" id="MyForm"
action="http://mydomain/page.aspx" method="post"> </form>
For cross-domain communication there is no easy client-side way for you to retrieve results. Server-side support would be required - exposing additional services that you can hit on the client (for example by embedding a element into the page).
This allows you to make GET requests to the server and get the result as JSON. One way to do it would be:
Client creates <script src="http://otherdomain.com/gettoken"></script>
This returns something like
var myToken = "ABC123";
This value is inserted into the form, which is then submitted to the same server
The server stores the results of the form under the key of the specified token
Client creates <script src="http://otherdomain.com/getresult?token=ABC123"></script>
Server fetches the form results for ABC123 and returns then as JSON
You need to make the request on the server side...
Dim Response as String
Using Client as New System.Net.WebClient()
Response = Client.UploadData("key=value")
End Using
Then if you want to use AJAX, you will point it to this local handler to bypass the cross-domain issue.
Related
I need to submit one form multiple times in parallel. The server accepts the parameter _ASYNCPOST.
I can explain in an abstract way how the page works
Login
Submit form search (POST)
POST same form with new data (all these need to be done in parallel)
In the last step, I yield all the requests with every parameter I could find (including __VIEWSTATE, EVENTTARGET, etc)
The problem is that the first post works, but the rest return an error saying "The server data does not match the browser data, hit refresh"
Is what I'm trying to achieve possible?
I followed this doc https://blog.scrapinghub.com/2016/04/20/scrapy-tips-from-the-pros-april-2016-edition/
Im using Visual Studio 2012 and creating a web page using "ASP.NET Web Site (Razor v2)"
Im using Java to generate a random link;
<script>
var random = new Array();
random[0] = "example1.com";
random[1] = "pattern1.com";
random[2] = "specimen1.com";
</script>
<script>
function randomlink() {
window.location = random[Math.floor(Math.random() * random.length)];
}
</script>
A Random URL
When I click the A Random URL link it opens a random page from the list in the script above. I'ts all good, but because its a verry big list I need a way to do the same without having it in HTML because its slowing the loading of the page since its in the _SiteLayout.cshtml. Thanks.
Among your choices are the following options:
Send all the URLs to the client and have the client pick a random choice.
Have the server pre-pick the random URL and send only that one to the client (it can just be put directly into the <a> link. No need for javascript at all.
Make an ajax call to the server to request a random link and when that is returned, go to it.
Make a get request to the server and have the server return a redirect to a randomly chosen URL.
It sounds like you don't want to implement the first option if you have a zillion URLs.
The second option is probably the easiest as it requires only slightly modifying the generation of the page and requires no new server APIs. You just have to figure out how to select a random URL in your server-side environment.
The third and fourth options are the least efficient as they require a call to the server, a response from the server with the new URL and then a client redirect to the actual URL.
I would pass the random url with the page when it renders from the server. You can generate the url on the server using c#'s Random class.
A Random URL
Just pass a model that you reference in your view.
I need to implement single page applications using ASP Web Forms. I faced with a navigation problem. I need to use a navigation pattern like this:
http:// web site url / ... / page.aspx? {query string} # {ListId} / {ItemId}
When a user request a data from the server, the request on the server doesn't contain hash # (because this is a client-side feature). And it looks like this:
http:// web site url / ... / page.aspx? {query string}
So, actually I need two requests:
to get a page without hash and load javascript;
to handle hash data using javascript and async call required data from the server.
Is it possible to implement this logic with only one request?
Are there any best practices?
You can append ListId/ItemId to query string before sending request and read it regularly on a server.
var url = 'http://example.com?param1=10¶m2=20#1000';
var beforeHash = url.split('#')[0];
var itemId= url.split('#')[1];
var processedUrl = beforeHash + '&itemId=' + itemId;
If your request is not already fired from JavaScript, you will have to hook into link's click event...
Or maybe you can get rid of # entirely and scroll content via JavaScript (my guess is that you use # because of local anchors to jump to different places in document)?
BTW There is window.location.hash property.
Update:
Based on your comment the flow is like this:
User types URL with #ItemId
Server returns the page
JavaScript reads #ItemId from window.location, puts it into QueryString and makes a request
Server returns the page based on modified QueryString
In this situation the two-requests pattern seems to be the only viable option. By design server does not get #Item part (called fragment). So there is no way to guess ItemId upon initial request. If after second (ajax) request, you refresh #ItemId dependant parts of the page through JavaScirpt, user experience will not be hindered much.
I want to redirect a response to another URL while it contains some POST data in it's HTTP header.
// Inside an ASP.NET page code behind:
Response.Redirect("http://www.example.com/?data=sent%20via%20GET");
// This will sent data to http://www.example.com via GET.
// I want to POST this data to http://www.example.com instead.
How to do this in ASP.NET?
you can send huge data also with this trick..
Response.Clear();
StringBuilder sb = new StringBuilder();
sb.Append("<html>");
sb.AppendFormat(#"<body onload='document.forms[""form""].submit()'>");
sb.AppendFormat("<form name='form' action='{0}' method='post'>",postbackUrl);
sb.AppendFormat("<input type='hidden' name='id' value='{0}'>", id);
// Other params go here
sb.Append("</form>");
sb.Append("</body>");
sb.Append("</html>");
Response.Write(sb.ToString());
Response.End();
You can't POST using a redirect. By definition a redirect means the server sending a 302 redirect HTTP status code to the client with the new location so that the client issues a GET request to this new location. That's how the HTTP protocol works and there's not mu ch you can do about it.
So one way to achieve this would be to redirect to some temporary page that will contain an HTML <form> with method="POST" and containing the values you want to send as hidden fields. Then you could use javascript to autosubmit this form.
Though it is quite old thread, I thought I would share how I do it.
Let's say sending page is a.aspx and destination page is b.aspx.
Collect user inputs in a.aspx. User clicks submit button which causes postback.
inside Button_click event of a.aspx, process volume data (for example save uploaded binary files). Determine link of the volume data and append that to end of request.form name-value string.
Encrypt the final name-value string and set it to a cookie.
Redirect to b.aspx
in b.aspx, retrieve that cookie, decrypt and you get all name-value pair. Now process them as usual.
Advantages:
(a) b.aspx is shown in browser address bar. It enters in browser's history. Server.transfer do these.
(b) It gives effect of post. Users can not see the name-value pair in querystring.
You can use viewstate to "transfer" the data and read it on a new page or even the same page.
I'm trying to post the entire asp.net form to a certain url.
I have tried:
$.post("http://www.someaddress.com", $("form").serialize());
I have also tried:
$.ajax({
type:"POST",
url:"http://www.someaddress.com",
data: $('form').serialize(),
success: function(){
alert('yay');
}
});
In both cases the submit is fine but no data is passed along with it.
When i test the form.serialize() in firebug console, this shows my form serialized just fine. When i view the submit in fiddler, i can see that the data part is not set. Maybe im not understanding the data part, but every single tutorial shows this as the way to go -> serialize the form and set that as data. What must i do to get my serialized form as the data in my request?
What am i missing? Also - why does the NET tab in firebug show all these requests as method OPTIONS?
is this
url:"http://www.someaddress.com"
just an example or do you try to access a foreign domain? (which would explain the problem).
Based on your comment, the ajax same origin policy does not allow to access a foreign domain.
You cannot do a Ajax request to a foreign domain. As this is not allowed (against security) in javascript to access a foreign page ( not on your domain )