How to POST cross-domain form data using DOJO - dojo

I want to send form data using POST method to a remote URL using DOJO.
dojo.xhrPost works for local domain & dojo.io.script.get is only for GET method.
Can anyone suggest me a method to do this?
Shiji

I believe you can use dojo.io.iframe to accomplish this:
dojo.io.iframe.send({
url: 'www.myawesome.server',
form: form,
content: content
});
But apparently you need to set the method on the form to POST:
dojo.attr(form, 'method', 'post');
(Or do it directly on the form: <form method="post" ...>)
I haven't tried this, but here is an article describing exactly what (I believe) you are trying to do:
http://www.mikejuniper.com/2009/03/fun-with-dojoioiframesend/

Related

submit button not working when generated from AJAX

I got an ASP NET Core RazorPage having a button which asynchronously replaces a part of the given HTML using an AJAX request.
Besides some text content it renders another button which is intended to post back the side when clicked. It is surrounded by a form element.
However, clicking the button I receive an HTTP 400 with the information "This page isn't working" (Chrome). Other browsers like Firefox return an HTTP 400 as well.
The relevant HTML with the button which has been created by the AJAX call is the one below:
<form method="post">
<button class="btnIcon" title="Todos" id="btnTodos" formaction="PersonManagement/Parts/MyPageName?handler=PerformTodos">Execute action</button>
</form>
As the url exists (I doublechecked it using the browser with a simple GET) I wonder whether the issue could be due to some security settings along with the browser or is there anything I am perhaps missing out here?
Thank you for any hint
Two things here first add this attribute to your form asp-antiforgery="true", then send it's value to the server in your AJAX post request.
jQuery magic starts here :)
token: $('[name=__RequestVerificationToken]').val(),
Antiforgery is ON by default since .net core 2.0 (as far as I remember), so if you do AJAX post you need to send the antiforgery token with each request.
Let us know if it helps. Spread knowledge don't hide it just for yourself :P
Finally I came across a very interesting article from Matthew Jones at https://exceptionnotfound.net/using-anti-forgery-tokens-in-asp-net-core-razor-pages/ about Anti-Forgery Tokens in Razor pages. Worth reading, indeed.
However, independently from that article what solved my issue was simply not to add the <form .. element at the client-side, but already at the server-side. As there is no need for me to explicitly adding it at the client-side, but only the button itself, this is a solution for me which works properly.
A brief summary of my scenario now:
There is a Razor Page containing usual cshtml content along with a <form method="post"..
Some anchor elements also are included, one is triggering a JQuery AJAX call to the server
The JQuery call comes back from the server with some additional HTML including the post button which which I add to the existing HTML.
The button is being rendered inside the now already existing
Clicking the button causes the page to post back in the wanted manner and executes the handler as intended.
Thanks again Stoyan for your input and help with that.

Access form post Data without request.Form

I know this is very weird but I get following error on my page and I know I can't have Request.form with ENCTYPE="multipart/form-data" Cannot call BinaryRead after using Request.Form collection.
My Actual Uploader page I use Forms collection to access form data like Uploader.Form("txtTitle").
But on this uploder page I have included couple of ASP pages which are standard thruout app for other security checks and etc and those pages use Request.form.
What's other alternative to access form post data without Request.form on those security pages? Since those are common pages use by all other pages which don't have ENCTYPE="multipart/form-data" on their forms.
Thanks in advance!
Put the includes' code which uses Request.Form inside a function, than pass values as parameters to the function instead of using Request.Form
So in your page it will look something like this:
<!--#INCLUDE FILE="myinclude.asp"-->
MyIncludeFunction Uploader.Form("MyIncludeFunctionParameter")
And in all other pages:
<!--#INCLUDE FILE="myinclude.asp"-->
MyIncludeFunction Request.Form("MyIncludeFunctionParameter")

Passing variables? wordpress

Hello all I'm very thankful for this community here, wouldn't know what to do without you all.
First of all, I'm not even sure if the title to this post is accurate; please read on. In a nutshell what I'm trying to do in Wordpress is create a 'reply' button that will be displayed on the individual post's page. When someone clicks on this 'reply' button it will take them to a different WP page that is using a private messaging plugin. On this page I would like the 'to:' field to automatically know who to reply to (author of the post).
Now here's my question. Is this accomplished by "passing a variable" from first page to the second or is there another way to do this?
I'm not asking for specific code help so please don't tell me to go talk with the plugin developer. I'm just trying to get a general idea of how something like can be accomplished so that I can do some research myself.
At the very least, If someone get give me a starting point for me to do some google research that would be all I need. Being fairly new I don't even know what phrase I should be googling for.
Yes, you can do that by implementing simple wp plugin. First of all you need to know that, there are lots of wp specific functions. You can use the_content for putting your reply link after post content, and get_author_meta in order to get post's author email for putting it in your custom link. I know, you don't want to talk about code, but I can give you sample example; In order to apply this functionality on all post, you can simply implement a plugin.
Edit: For redirecting to Private Messaging plugin's send page with prepopulated recipient field, I have updated get_the_author_meta('email') with get_the_author_meta('user_login'). Now, you can go to mail send page, by clicking Reply link
add_filter('the_content', 'add_custom_link');
function add_custom_link($content) { // You can think that $content => individual post
if(is_single()) {
$content .= 'Reply to this post';
}
return $content;
}
Save this code in a php file and zip it.Then, upload it to your wp site as plugin. And you will see your custom link at the end of your posts. Do not forget to update variables in plugin according to your needs(for example reply url domain)
Here is a working plugin demo: http://huseyinbabal.eu01.aws.af.cm/?p=1
Create a form that posts to the secondary page.
Use a hidden form field to pass the post ID
Make sure you prefix the form fields so your $_POST variable doesn't conflict with any other core/plugin variables
On the secondary page: make sure you sanitize that user input before you do anything else
with it
use the sanitized post id to look up the post's author, without
having to expose the author's email in the url.
Your Form should look something like so:
<form action="url-of-page-2" method="POST">
<input type="hidden" name="myprefix_id" value="<?php echo get_the_ID();?>">
<input type="submit" value="reply">
</form>

Setting returnURL for CButtonColumn button

I'm looking at the controller for the default Delete button in the CButtonColumn class. It manages to return to the previous web-page after deleting a CGridView line and remain on the same page of the CGridView, as opposed to going to the first page. The lines responsible for this in the associated controller seem to be:
if (!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
I would like to create a new custom button that has this behavior (i.e. returning to the previous view without resetting the pagination to page 1), but simply including the above lines of code in the button's associated action does not do the trick. I think I need to send that 'returnUrl' parameter somehow, but I cannot figure out how :)
The 'returnUrl' code you are looking at uses a POST variable for the returnUrl. To use this, you will need to POST that somehow. On the View this code is called from I am assuming there is a <input name="returnUrl"> field in the form. You should make sure this field (populated with the correct URL value) is on all of the Views you are POSTing from in order to access that POST variable in your Controller action.
If you are POSTing to the deleteAction via AJAX, I think you can set the $_POST['returnUrl'] variable with the jQuery AJAX function.
Another way to go might be to use CWebUser's returnUrl SESSION variable instead of this POST variable. I have never done this, but it's built in to Yii so I assume it works OK.
I never really liked the hacky $_POST['returnUrl'] that Gii generates anyway.
ANOTHER thing you could do, possibly, is look at the $_SERVER['HTTP_REFERER'] variable, and use that for the return redirect in your deleteAction. I don't know if that will be set correctly though, with complications from the 302 redirect/rewrites that Yii does.
Good luck!
You can set the return url via the CHtml::link call. Here is an example using delete
CHtml::link(
'Delete',
'#',
array('submit'=>array('delete','id'=>$model->id),
'params'=>('returnUrl'=>'controller/action...'),
'confirm' => 'Are you sure?'
)
);
Pulled from this Stackoverflow answer.

POST a HTML Form programmatically?

I need to POST a HTML form to a 3rd party website (a Mass-SMS texting system).
In the past I've done this by forwarding to a page containing a form I've pre-populated and hidden (using display:none), then I've ran a javascript function at the end of the page to automatically submit this form.
However I'm hoping theres someway I can do all this programmatically (as I don't care about the response, and the user doesn't need to see the page the form is being posted to).
How can I do this? Cheers
You could use a WebClient.UploadValues method to send an HTTP POST request to a remote server from your code behind. Just fill up the name/value collection with the values coming from the hidden fields.
If you're willing to get into PHP, you can very easily use cURL for this.
Otherwise it's going to be quite difficult using just Javascript.
See here for a detailed tutorial.