gpt4 book ai didi

javascript - jquery ajax 未在 codeigniter 中发送数据

转载 作者:行者123 更新时间:2023-11-28 04:35:16 26 4
gpt4 key购买 nike

我是 Codeigniter MVC 框架的新手。当我通过ajax从views发送数据到controller时,此错误显示click here to view the image

这是我的代码:

views/ajax_post_view.php:

 <!DOCTYPE html>
<html lang="en">
<head>
<title>Ajax Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script>
$(document).ready(function(){
$(".submit").click(function(e){
e.preventDefault();

var user_name = $("input#name").val();
var password = $("input#pwd").val();

//alert(user_name);
//alert(password);

$.ajax({
type:'POST',
url:'<?php echo base_url();?>'+'index.php/ajax_controller/submit',
dataType:'json',
data:{name:user_name,pwd:password},
success:function(data){
console.log(data);
}
});
});
});
</script>
</head>
<body>
<div class="main">
<div id="content">
<h2 id="form-head">Pavan Code Igniter Ajax</h2>
<hr>

<div id="form_input">
<?php

echo form_open();

echo form_label('User Name');
$data_name = array(
'name'=>'name',
'class'=>'input_box',
'placeholder'=>'Please enter name',
'id'=>'name'
);
echo form_input($data_name);
echo "<br>";
echo "<br>";

echo form_label('Password');
$data_name = array(
'type'=>'password',
'name'=>'pwd',
'class'=>'input_box',
'placeholder'=>'Please enter Password',
'id'=>'pwd'
);
echo form_input($data_name);
?>
</div>

<div id="form_button">
<?php echo form_submit('submit','Submit','class="submit"');?>
</div>

<?php
echo form_close();
?>
</div>
</div>
</body>
</html>

Controller :controllers/Ajax_controller.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax_controller extends CI_Controller {

// Show view Page
public function index(){
$this->load->helper('form');
$this->load->helper('url');
$this->load->view("ajax_post_view");
}

// This function call from AJAX
public function submit() {
$data = array(
'username' => $this->input->post('name'),
'pwd'=>$this->input->post('pwd')
);
echo json_encode($data);
}
}
?>

请帮助我提前致谢

最佳答案

在 Controller 中,在 php 脚本顶部设置 Access-Control-Allow-Origin :

header('Access-Control-Allow-Origin: *');

关于javascript - jquery ajax 未在 codeigniter 中发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44277532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com