简体   繁体   中英

jQuery post method parameter passing fail

I am new to Javascript and learning POST method. I wrote some code, but it doesn't work and I can't figure it out why.

Here is script code:

<script>
    $(document).ready(function () {
        $.post("/api/totaluserexp",
        {
            startDate: '20150701',
            endDate: '20150701'
        },
        function (data) {
            document.write("Total Experience: " + data);
        }, 'json');
    });
</script>

Here is another script code, should do the same thing:

<script>
    $(document).ready(function () {
        $.post("/api/totaluserexp",
            {
                startDate: "20150701",
                endDate: "20150701"
            })
            .done(function (data) {
                $(".result").html("Total Experience: " + data);
            });
    });
</script>

Both of them returns an empty page with no errors, no warnings etc. Even when I use alert , nothing happens.

Here is my controller:

public int Post(DateTime startDate, DateTime endDate)
{
    return DBClassPackage.izamanraporlama.getUserTotalExp(startDate, endDate);
}

Set a variable in your controller to debug the input data and output result.

public int Post(DateTime startDate, DateTime endDate)
{
    int result = DBClassPackage.izamanraporlama.getUserTotalExp(startDate, endDate);

    return result;
}

I figured it out. I tried to return a simple integer (like return 5;) but it also didn't worked.

I don't know why, but this Post method is not the Post method it should be. I managed to pass parameters in the url like this:

<script>
    $(document).ready(function () {
        $.post("/api/totaluserexp/apiden?20150701&20150701",
            {
                // startDate: "20150701",
                // endDate: "20150701"
            })
            .done(function (data) {
                $(".result").html("Total Experience: " + data);
            });
    });
</script>

If somebody explain why, I will accept his/her answer, cause I am very curious.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM