我遇到了一个不寻常的问题,我非常感谢你的帮助。我正在使用CodeIgniter并尝试从视图中进行Ajax调用。问题是当我选择类型:“POST”时,我总是在控制台中收到错误提示,POST http://localhost/scratchcard/index.php/ajax/ajax/create?callback=jQuery1910195569250266999_1365021602115
500(内部服务器错误)。
我的观点是这样的:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="shortcut icon" href="http://localhost/scratchcard//images/favicon.ico" />
<link rel="stylesheet" href="http://localhost/scratchcard//css/demo.css" type="text/css" media="screen" title="no title" charset="utf-8"/>
<script src="http://localhost/scratchcard/js/jquery-1.9.1.js" type="text/javascript" charset="utf-8"></script>
<script src="http://localhost/scratchcard/js/yui.formalize.js" type="text/javascript" charset="utf-8"></script>
<script src="http://localhost/scratchcard/js/scratchcard.js" type="text/javascript" charset="utf-8"></script>
<input type="hidden" name="url" id="url" value="http://localhost/scratchcard/" />
<meta charset="utf-8"><div id="main_container">
<div id="create_scratch_card" align="center">
<label id="form_count">Create New Scratch Cards</label>
<select id="count_select">
<option value=""> Select the number of cards to be generated </option>
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<br /><p> </p>
<label id="form_amount">Amount of each Card</label>
<select id="amount_select">
<option value="" > Select the amount of the cards to be genarated</option>
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<br /><p> </p>
<input type="button" id="create_button" value="Create">
</div>
<div id="ajax_result" align="center">
<img src="http://localhost/scratchcard/images/ajax-loader.gif" style="max-width:100%;" alt="Processing">
</div>
</div>
</body>
</html>
我的ScratchCard.js是这样的:
$(document).ready(function(){
var url_link=$("#url").val();
$("#create_button").click(function(){
/*$.ajax({
url:"<?=base_url()?>index.php/ajax/ajax/create",
type:"POST",
data:{
count: $("#count_select").val();
amount:$("#form_amoutn").val();
}
});*/
if($("#count_select option:selected").val().length==0){
alert("You Must Select a Count Value first");
$("#count_select").focus();
}
else if($("#amount_select option:selected").val().length==0)
{
alert("You Must Select a Amount Value first");
$("#amount_select").focus();
}
else{
$.ajax({
type:"POST",
dataType: 'jsonp',
data:{
count: $("#count_select option:selected").val(),
amount:$("#amount_select option:selected").val()
},
url:url_link+"index.php/ajax/ajax/create",
beforeSend:function(){
$("ajax_result").show();
},
complete:function(response){
alert(response.reply);
},
});
}
});
});
最后是Ajax.php:
<?php
/**
*
*/
class ajax extends CI_Controller {
public function create(){
$this->output->set_status_header('200');
$this->output->set_header("Content-Type: application/javascript");
$this->output
->set_content_type('application/json')
->set_output(json_encode(array('reply' => 'created')));
}
}
?>
你能否指出我所犯的任何错误或解决方法?
答案 0 :(得分:0)
数据类型应仅为json
dataType: 'json'
jsonp适用于跨域,您的网址在同一个域中,并且您可以使用的数据
data:{count: $("#count_select").val(),amount:$("#amount_select").val()}