0

我遇到了一个不寻常的问题,非常感谢您的帮助。我正在使用 CodeIgniter 并尝试从视图中进行 Ajax 调用。问题是当我选择类型:“POST”时,我总是http://localhost/scratchcard/index.php/ajax/ajax/create?callback=jQuery1910195569250266999_1365021602115在控制台中收到一条错误消息,POST 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')));


    }
}
 ?>

你能指出我犯的任何错误或如何解决它的任何解决方案吗?

4

1 回答 1

0

数据类型应仅为 json

dataType: 'json'

jsonp 用于跨域,您的 url 看起来像它在同一个域中,对于您可以执行的数据

data:{count: $("#count_select").val(),amount:$("#amount_select").val()}
于 2013-04-03T20:53:37.417 回答