Difference between form post and jquery post

I need to know the exact difference between:

<form method="POST" action="https://mywebsite/signon.php"> <input name="harv_acc" value="940322903" type="hidden" /> <input name="harv_eml" value=" a@b.com " type="hidden" /> <input type="submit" value="SignOn" /> 

and

 var url = "https://mywebsite/signon.php"; $.ajax({ url: url, type: 'POST', //dataType: 'html', -- this was something I tried later //data: "harv_acc=" + accountnumber + "&harv_eml=" + email , this is also what I tried last but below is what I tried first data: { harv_acc: account, harv_eml: email }, success: function (data) { closePopup("div_PleaseWait"); alert(data); //window.location = encodeURI('<%= Url.Action("DownloadDocument", "Documents") %>?DocumentID=' + documentID + '&DownloadType=' + downloadType + '&DownloadPath=' + data); } }); 

When I send the last one, I get 200, but I don’t reply. If I send the first, I will get the correct answer.

+4
source share
5 answers

From the comments:

I am sending to another site

Yeah! Your problem is there. Browsers block AJAX for external websites for security reasons. Sorry, but you are not going to issue this request using the XHR request.

If another website wants you to communicate with them, they could expose this part of the site through JSON-P, which works something like this:

  • My site adds <script src="http://othersite.com/signon.js?username=foo&password=bar&callback=myCallback"> to the source code (yes, it's useless to use GET for this, but JSON-P cannot work somehow in another way) and creates a function called myCallback to process the response data.
  • Another site signs up, then returns something like myCallback({success: false, errorMessage: "Incorrect password, try again!"})
  • This script runs on my site, calls myCallback , and all is happy.

JSON-P is a powerful protocol, but only works if the remote site agrees with it. However, if they do, jQuery has a nice shortcut for it: just set dataType: "jsonp" and it will handle the whole callback function for you.

But if you are not closely associated with this site, it is unlikely to happen, and you probably just want to abandon this interaction between sites. Sorry, but such a cross-domain policy is crucial for online security. (I do not want other sites to issue bankofamerica.com requests on my behalf, thank you.)

+14
source

The first parameter passed to your complete function will be the jqXHR object, which is a wrapper around the browser XMLHttpRequest object. A more convenient way to handle the response is to use the done method:

 var url = "https://mywebsite/signon.php"; $.ajax({ url: url, type: 'POST', dataType: 'html', data: "harv_acc=" + accountnumber + "&harv_eml=" + email }).done(function(data) { closePopup("div_PleaseWait"); alert(data); }); 
+1
source

Try sending data as an object: value. This is an example from jQuery docs

  $.ajax({ type: "POST", url: "some.php", data: { name: "John", location: "Boston" } }).done(function( msg ) { alert( "Data Saved: " + msg ); }); 

Update: as indicated in Matchu, this is not a problem, since the data will still be converted to a query string, as indicated in jQuery docs:

"The data option can contain either a query string of the form key1 = value1 & key2 = value2, or a map of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted to the query string using jQuery.param ( ) before sending."

So, some kind of rash is responding to my role there. At least I found out something !;)

0
source

Ajax redirect requests are not supported by the browser. But there is another way around this.

You can use JSONP for cross-domain requests. It is easy to use and allows you to request anything (as long as it is in JSON format) from any server / script that supports callbacks. The good thing about JSONP is that it works in older browsers too.

The only serious limitation seems to be that it always uses the HTTP GET method

You can also check this out.

0
source

when using the POST method, you must in your case send your data as JSON

 var url = "https://mywebsite/signon.php"; $.ajax({ url: url, type: 'POST', dataType: 'html', data: { harv_acc : accountnumber, harv_eml : email }, success: function (data) { closePopup("div_PleaseWait"); alert(data); //window.location = encodeURI('<%= Url.Action("DownloadDocument", "Documents") %>?DocumentID=' + documentID + '&DownloadType=' + downloadType + '&DownloadPath=' + data); } }); 

NOTE: I used dataType: JSON

-2
source

All Articles