1

The function onclick call properly hides and shows elements but when it comes to the console.log or the ajax Post method nothing is working. I have tried it in Postman and it was working properly when i sent json.

Script:

<script type="text/javascript">
    function populateTab() {

    }
    var addvisible = document.getElementById("btnshowadd");
    $("#addpostform").hide();
    addvisible.onclick = function () { $("#addpostform").show() };
    $("#hideadd").onclick = function () {

        var postObject = {
            Title: $("#PostTitle").val(),
            Body: $("#PostBody").val(),
            Image: $("#PostImage").val(),
            DateTime: Date.now()
        };
        console.log(postObject);
        $("#addpostform").hide();
        jQuery.ajax({
            type: "POST",
            data :postObject,
            url: "../api/posts",
            contentType: "application/json"

        });
    };

</script>

Post method in web api

[ResponseType(typeof(Post))]
[HttpPost]
public IHttpActionResult PostPost(Post post)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    db.Posts.Add(post);
    db.SaveChanges();

    return CreatedAtRoute("DefaultApi", new { id = post.ID }, post);
}
Taha Mouhtij
  • 57
  • 1
  • 8
  • 1
    Do you see any error in the console? – litelite Aug 23 '17 at 17:29
  • no, when i add console.log in the function, nothing is working either – Taha Mouhtij Aug 23 '17 at 17:30
  • 1
    Please define "_nothing is working_". What you expect your code to do, and what it does instead? – Teemu Aug 23 '17 at 17:30
  • 1
    `$("hideadd")` uses jquery to search for html tags . I suspect you either have a class or an id called hideadd, so you would need to use either `$(".hideadd")` or `$("#hideadd")`, respectively – James Aug 23 '17 at 17:30
  • @Teemu when i wanna console log the object it dosnt work, and the ajax method isnt working either – Taha Mouhtij Aug 23 '17 at 17:31
  • Sidenote : is it normal that the `populateTab` function is empty? – litelite Aug 23 '17 at 17:32
  • I must be blind, I can't see any `console.log` in the code, also it seems the ajax callback function is missing ..? – Teemu Aug 23 '17 at 17:33
  • @James didnt see that, corrected it still not working :( – Taha Mouhtij Aug 23 '17 at 17:33
  • @Teemu it doesn't work even if its not in the code just didn't put it on this version, and i read this answer for posting https://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-object – Taha Mouhtij Aug 23 '17 at 17:34
  • @TahaMouhtij How you create `postObject` have the same problem. You are asking JQuery to find a `` tag. All the fields will be undefined and that might cause some problem on your backend. Did you take a look at the network tab to see the status of your query and whether it is actually fired? – litelite Aug 23 '17 at 17:34
  • @litelite edited the code how it's looking on my ide and still not doing the console.log or the jquery ajax call – Taha Mouhtij Aug 23 '17 at 17:38
  • @TahaMouhtij Did you try to put a breakpoint and step through the code? – litelite Aug 23 '17 at 17:40
  • 1
    `$("#hideadd").onclick` ... You're mixing jQuery and vanilla JS, should be `$("#hideadd").click(function () {...});` – Teemu Aug 23 '17 at 17:40
  • @litelite i put a break point on the web api but never being called, the thing is the $("#addpostform").hide(); in the function is working when i click on submit, but the console.log and post method is not. – Taha Mouhtij Aug 23 '17 at 19:20
  • @Teemu i see no difference when i changed the syntax – Taha Mouhtij Aug 23 '17 at 19:20
  • @TahaMouhtij No difference? `$("#hideadd")` returns a jQuery object, `$("#hideadd").onclick` just adds a custom property to it, it doesn't attach an event listener to any element. Clear the cache ... – Teemu Aug 24 '17 at 03:56
  • @Teemu you were right, it was just displaying the elements so fast on the console.log and clearing itself cause of bad configuration of the post method , thanks – Taha Mouhtij Aug 25 '17 at 05:01

0 Answers0