问题描述
我正在尝试使用Ajax提交表单,但是单击按钮时,控制器中的操作永远不会被击中。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <term.h>
#include <readline/readline.h>
int execute_line(char *line);
void initialize_readline();
static char **fileman_completion(char *text,int start,int end);
static char *command_generator(char *text,int state);
char *command[] = { "view","quit",(char *)NULL };
int done; /* When non-zero,this global means the user is done using this program. */
int main(int argc,char **argv)
{
char *line;
initialize_readline(); /* Bind our completer. */
for ( ; done == 0; ) {
line = readline("> ");
if (!line) break;
if (*line)
execute_line(line);
free(line);
}
return 0;
}
/* String to pass to system(). This is for the VIEW command. */
static char syscom[1024];
int execute_line(char *line)
{
int i = 0;
char *word;
/* Isolate the command word. */
while (line[i] && whitespace(line[i]))
i++;
word = line + i;
while (line[i] && !whitespace(line[i])) i++;
if (line[i]) line[i++] = '\0';
if (strcmp(word,"quit") == 0) {
done = 1;
return 0;
} else if (strcmp(word,"view")) {
fprintf(stderr,"%s: Choose only \"view FILE\" or \"quit\" as your command.\n",word);
return -1;
}
/* Get argument to command,if any. */
while (whitespace(line[i])) i++;
word = line + i;
if(!word || !*word) {
fprintf(stderr,"view: Argument required.\n");
return -1;
}
sprintf(syscom,"more %s",word);
return system(syscom);
}
static void display_matches(char** matches,int len,int max)
{
int saved_point = rl_point;
char *saved_line = rl_copy_text(0,rl_end);
rl_save_prompt();
rl_replace_line("",0); // Clear the prevIoUs text
rl_display_match_list(matches,len,max);
putp(cursor_up);
rl_restore_prompt();
rl_replace_line(saved_line,0);
rl_point = saved_point;
rl_redisplay();
free(saved_line);
}
void initialize_readline()
{
rl_readline_name = "rline";
rl_attempted_completion_function = (rl_completion_func_t *)fileman_completion;
rl_completion_display_matches_hook = display_matches;
setupterm(NULL,1,(int*)0);
}
static char **fileman_completion(char *text,int end)
{
if (start == 0)
return rl_completion_matches(text,(rl_compentry_func_t *)*command_generator);
return NULL;
}
static char *command_generator(char *text,int state)
{
static int list_index,len = 0;
char *name;
if (!state) {
list_index = 0;
len = strlen(text);
}
if (len > 0)
while ((name = command[list_index++]))
if (strncmp(name,text,len) == 0)
return strdup(name);
return NULL;
}
我最初使用如下所示的Ajax标签帮助程序创建了表单,但由于现在能够停止表单重定向,因此不得不更改为在javascript中使用Ajax
$('#sendCodeForm').submit(function (e) {
var form = $('sendCodeForm')[0];
var formData = new FormData(form);
if ($(this).valid()) {
e.preventDefault();
$.ajax({
url: '@Url.Action("SendResetCode","Home")',type: 'post',cache: false,processData: false,contentType: false,data: formdata,success: function (data) {
if (data.status == "success") {
$("#successMessage").append('Reset code has been sent to');
}
}
});
}
});
<form id="sendCodeForm">
<label for="email">EMAIL ADDRESS</label>
<input asp-for="username" id="username" type="email" placeholder="your@email.com" /><br>
<button type="submit" id="sendCodeBtn">Send Code</button>
</form>
解决方法
您可以尝试如下更改代码:
$('#sendCodeForm').submit( function (e) {
e.preventDefault();
var formData = new FormData($('#sendCodeForm')[0]);
$.ajax({
url: '@Url.Action("SendResetCode","Home")',type: 'post',cache: false,processData: false,contentType: false,data: formData,success: function (data) {
if (data.status == "success") {
$("#successMessage").append('Reset code has been sent to');
}
}
});
});