Finding it impossible to post simple ajax





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am realy struggling with this and would apprecate any advice.



I have this field



<input id="scanInput" type="text" placeholder="SCAN" class="form-control" />


For which I would like to make an ajax call when the field changes, so I tried



<script>
$("#scanInput").change(function () {
console.log('ye');

$.getJSON("?handler=GetPartDetails", function (data) {
//Do something with the data.
console.log('yay?');
}).fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.log("Request Failed: " + err);
});
});
</script>


Where my PageModel has this method



[HttpPost]
public IActionResult GetPartDetails()
{
return new JsonResult("hello");
}


and the url for the page in question is /packing/package/{id}



Now when I change the input value, I see ye on the console, and I can see that the network called http://localhost:7601/Packing/Package/40?handler=GetPartDetails (the correct URL I think?) with status code 200



But My breakpoint in GetPartDetails never hits, and I don't see yay? in the console.



I also see this message from the fail handler:




Request Failed: parsererror, SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data




But I'm not even passing any JSON data... why must it do this
I also tried this way :



$.ajax({
type: "POST",
url: "?handler=GetPartDetails",
contentType : "application/json",
dataType: "json"
})


but I get




XML Parsing Error: no element found
Location: http://localhost:7601/Packing/Package/40?handler=GetPartDetails
Line Number 1, Column 1:




I also tried



$.ajax({
url: '/?handler=Filter',
data: {
data: "input"
},
error: function (ts) { alert(ts.responseText) }
})
.done(function (result) {
console.log('done')
}).fail(function (data) {
console.log('fail')
});


with Action



    public JsonResult OnGetFilter(string data)
{
return new JsonResult("result");
}


but here I see the result text in the console but my breakpoint never hits the action and there are no network errors..............
What am I doing wrong?










share|improve this question

























  • GetPartDetails is a query string parameter or an action method name? Also handler is a query parameter name or what?

    – Kunal Mukherjee
    Nov 16 '18 at 15:14











  • Please see the question - GetPartDetails is a method returning IActionResult in my PageModel...

    – Bassie
    Nov 16 '18 at 15:15











  • what is handler?=?

    – Kunal Mukherjee
    Nov 16 '18 at 15:16













  • I got it from here: talkingdotnet.com/… - it should be the name of the method I think

    – Bassie
    Nov 16 '18 at 15:17








  • 1





    I don't know if this is the problem but it looks like your client code is doing a "GET" but your controller action has the HttpPost attribute?

    – DaveG
    Nov 16 '18 at 15:31


















1















I am realy struggling with this and would apprecate any advice.



I have this field



<input id="scanInput" type="text" placeholder="SCAN" class="form-control" />


For which I would like to make an ajax call when the field changes, so I tried



<script>
$("#scanInput").change(function () {
console.log('ye');

$.getJSON("?handler=GetPartDetails", function (data) {
//Do something with the data.
console.log('yay?');
}).fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.log("Request Failed: " + err);
});
});
</script>


Where my PageModel has this method



[HttpPost]
public IActionResult GetPartDetails()
{
return new JsonResult("hello");
}


and the url for the page in question is /packing/package/{id}



Now when I change the input value, I see ye on the console, and I can see that the network called http://localhost:7601/Packing/Package/40?handler=GetPartDetails (the correct URL I think?) with status code 200



But My breakpoint in GetPartDetails never hits, and I don't see yay? in the console.



I also see this message from the fail handler:




Request Failed: parsererror, SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data




But I'm not even passing any JSON data... why must it do this
I also tried this way :



$.ajax({
type: "POST",
url: "?handler=GetPartDetails",
contentType : "application/json",
dataType: "json"
})


but I get




XML Parsing Error: no element found
Location: http://localhost:7601/Packing/Package/40?handler=GetPartDetails
Line Number 1, Column 1:




I also tried



$.ajax({
url: '/?handler=Filter',
data: {
data: "input"
},
error: function (ts) { alert(ts.responseText) }
})
.done(function (result) {
console.log('done')
}).fail(function (data) {
console.log('fail')
});


with Action



    public JsonResult OnGetFilter(string data)
{
return new JsonResult("result");
}


but here I see the result text in the console but my breakpoint never hits the action and there are no network errors..............
What am I doing wrong?










share|improve this question

























  • GetPartDetails is a query string parameter or an action method name? Also handler is a query parameter name or what?

    – Kunal Mukherjee
    Nov 16 '18 at 15:14











  • Please see the question - GetPartDetails is a method returning IActionResult in my PageModel...

    – Bassie
    Nov 16 '18 at 15:15











  • what is handler?=?

    – Kunal Mukherjee
    Nov 16 '18 at 15:16













  • I got it from here: talkingdotnet.com/… - it should be the name of the method I think

    – Bassie
    Nov 16 '18 at 15:17








  • 1





    I don't know if this is the problem but it looks like your client code is doing a "GET" but your controller action has the HttpPost attribute?

    – DaveG
    Nov 16 '18 at 15:31














1












1








1








I am realy struggling with this and would apprecate any advice.



I have this field



<input id="scanInput" type="text" placeholder="SCAN" class="form-control" />


For which I would like to make an ajax call when the field changes, so I tried



<script>
$("#scanInput").change(function () {
console.log('ye');

$.getJSON("?handler=GetPartDetails", function (data) {
//Do something with the data.
console.log('yay?');
}).fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.log("Request Failed: " + err);
});
});
</script>


Where my PageModel has this method



[HttpPost]
public IActionResult GetPartDetails()
{
return new JsonResult("hello");
}


and the url for the page in question is /packing/package/{id}



Now when I change the input value, I see ye on the console, and I can see that the network called http://localhost:7601/Packing/Package/40?handler=GetPartDetails (the correct URL I think?) with status code 200



But My breakpoint in GetPartDetails never hits, and I don't see yay? in the console.



I also see this message from the fail handler:




Request Failed: parsererror, SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data




But I'm not even passing any JSON data... why must it do this
I also tried this way :



$.ajax({
type: "POST",
url: "?handler=GetPartDetails",
contentType : "application/json",
dataType: "json"
})


but I get




XML Parsing Error: no element found
Location: http://localhost:7601/Packing/Package/40?handler=GetPartDetails
Line Number 1, Column 1:




I also tried



$.ajax({
url: '/?handler=Filter',
data: {
data: "input"
},
error: function (ts) { alert(ts.responseText) }
})
.done(function (result) {
console.log('done')
}).fail(function (data) {
console.log('fail')
});


with Action



    public JsonResult OnGetFilter(string data)
{
return new JsonResult("result");
}


but here I see the result text in the console but my breakpoint never hits the action and there are no network errors..............
What am I doing wrong?










share|improve this question
















I am realy struggling with this and would apprecate any advice.



I have this field



<input id="scanInput" type="text" placeholder="SCAN" class="form-control" />


For which I would like to make an ajax call when the field changes, so I tried



<script>
$("#scanInput").change(function () {
console.log('ye');

$.getJSON("?handler=GetPartDetails", function (data) {
//Do something with the data.
console.log('yay?');
}).fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.log("Request Failed: " + err);
});
});
</script>


Where my PageModel has this method



[HttpPost]
public IActionResult GetPartDetails()
{
return new JsonResult("hello");
}


and the url for the page in question is /packing/package/{id}



Now when I change the input value, I see ye on the console, and I can see that the network called http://localhost:7601/Packing/Package/40?handler=GetPartDetails (the correct URL I think?) with status code 200



But My breakpoint in GetPartDetails never hits, and I don't see yay? in the console.



I also see this message from the fail handler:




Request Failed: parsererror, SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data




But I'm not even passing any JSON data... why must it do this
I also tried this way :



$.ajax({
type: "POST",
url: "?handler=GetPartDetails",
contentType : "application/json",
dataType: "json"
})


but I get




XML Parsing Error: no element found
Location: http://localhost:7601/Packing/Package/40?handler=GetPartDetails
Line Number 1, Column 1:




I also tried



$.ajax({
url: '/?handler=Filter',
data: {
data: "input"
},
error: function (ts) { alert(ts.responseText) }
})
.done(function (result) {
console.log('done')
}).fail(function (data) {
console.log('fail')
});


with Action



    public JsonResult OnGetFilter(string data)
{
return new JsonResult("result");
}


but here I see the result text in the console but my breakpoint never hits the action and there are no network errors..............
What am I doing wrong?







c# asp.net-core razor-pages






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 15:45







Bassie

















asked Nov 16 '18 at 15:12









BassieBassie

4,0562356




4,0562356













  • GetPartDetails is a query string parameter or an action method name? Also handler is a query parameter name or what?

    – Kunal Mukherjee
    Nov 16 '18 at 15:14











  • Please see the question - GetPartDetails is a method returning IActionResult in my PageModel...

    – Bassie
    Nov 16 '18 at 15:15











  • what is handler?=?

    – Kunal Mukherjee
    Nov 16 '18 at 15:16













  • I got it from here: talkingdotnet.com/… - it should be the name of the method I think

    – Bassie
    Nov 16 '18 at 15:17








  • 1





    I don't know if this is the problem but it looks like your client code is doing a "GET" but your controller action has the HttpPost attribute?

    – DaveG
    Nov 16 '18 at 15:31



















  • GetPartDetails is a query string parameter or an action method name? Also handler is a query parameter name or what?

    – Kunal Mukherjee
    Nov 16 '18 at 15:14











  • Please see the question - GetPartDetails is a method returning IActionResult in my PageModel...

    – Bassie
    Nov 16 '18 at 15:15











  • what is handler?=?

    – Kunal Mukherjee
    Nov 16 '18 at 15:16













  • I got it from here: talkingdotnet.com/… - it should be the name of the method I think

    – Bassie
    Nov 16 '18 at 15:17








  • 1





    I don't know if this is the problem but it looks like your client code is doing a "GET" but your controller action has the HttpPost attribute?

    – DaveG
    Nov 16 '18 at 15:31

















GetPartDetails is a query string parameter or an action method name? Also handler is a query parameter name or what?

– Kunal Mukherjee
Nov 16 '18 at 15:14





GetPartDetails is a query string parameter or an action method name? Also handler is a query parameter name or what?

– Kunal Mukherjee
Nov 16 '18 at 15:14













Please see the question - GetPartDetails is a method returning IActionResult in my PageModel...

– Bassie
Nov 16 '18 at 15:15





Please see the question - GetPartDetails is a method returning IActionResult in my PageModel...

– Bassie
Nov 16 '18 at 15:15













what is handler?=?

– Kunal Mukherjee
Nov 16 '18 at 15:16







what is handler?=?

– Kunal Mukherjee
Nov 16 '18 at 15:16















I got it from here: talkingdotnet.com/… - it should be the name of the method I think

– Bassie
Nov 16 '18 at 15:17







I got it from here: talkingdotnet.com/… - it should be the name of the method I think

– Bassie
Nov 16 '18 at 15:17






1




1





I don't know if this is the problem but it looks like your client code is doing a "GET" but your controller action has the HttpPost attribute?

– DaveG
Nov 16 '18 at 15:31





I don't know if this is the problem but it looks like your client code is doing a "GET" but your controller action has the HttpPost attribute?

– DaveG
Nov 16 '18 at 15:31












3 Answers
3






active

oldest

votes


















0














Excuse me for posting this answer, I'd rather do this in the comment section, but I don't have the privilege yet.



Shouldn't your PageModel look like this ?






[HttpPost]
public IActionResult GetPartDetails() {
return new JsonResult {
Text = "text", Value = "value"
};
}








share|improve this answer
























  • Thanks for your answer, but it doesnt let me instantiate JsonResult like that - it just says TExt and Value dont exist

    – Bassie
    Nov 16 '18 at 15:43



















0














Somehow I found a setup that works but I have no idea why..



PageModel



    [HttpGet]
public IActionResult OnGetPart(string input)
{
var bellNumber = input.Split('_')[1];
var partDetail = _context.Parts.FirstOrDefault(p => p.BellNumber == bellNumber);

return new JsonResult(partDetail);
}


Razor Page



$.ajax({
type: 'GET',
url: "/Packing/Package/" + @Model.Package.Id,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
input: barcode,
handler: 'Part'
},

success: function (datas) {
console.log('success');
$('#partDescription').html(datas.description);
}
});





share|improve this answer





















  • 1





    It works because you have stumbled on the correct approach. It's worth spending a little time researching how handler methods work (learnrazorpages.com/razor-pages/handler-methods), and how to use the jQuery ajax method (api.jquery.com/jquery.ajax). Also, there is no place for the [HttpGet] or [HttpPost] attributes in Razor Pages. They belong to MVC and have no effect at all in Razor Pages.

    – Mike Brind
    Nov 16 '18 at 16:06











  • @MikeBrind Thanks for the tip Mike - I'll check out those pages

    – Bassie
    Nov 16 '18 at 16:07













  • return new JsonResult("hello"); was most likely your problem, because "hello" is not a valid Json object.

    – Adam Vincent
    Nov 16 '18 at 16:10











  • @AdamVincent But then wouldn't the breakpoint still go to the method?

    – Bassie
    Nov 16 '18 at 16:11











  • @AdamVincent the main error was that the AJAX call was not being made to the correct page. Then in the first example, the handler method name did not follow convention and would never get selected by the framework.

    – Mike Brind
    Nov 16 '18 at 17:02



















0














For this issue, it is related with the Razor page handler. For default handler routing.




Handler methods for HTTP verbs ("unnamed" handler methods) follow a
convention: On[Async] (appending Async is optional but
recommended for async methods).




For your original post, to make it work with steps below:




  • Change GetPartDetails to OnGetPartDetails which handler is PartDetails and http verb is Get.

  • Remove [HttpPost] which already define the Get verb by OnGet.


  • Client Request



     @section Scripts{ 
    <script type="text/javascript">
    $(document).ready(function(){
    $("#scanInput").change(function () {
    console.log('ye');

    $.getJSON("?handler=PartDetails", function (data) {
    //Do something with the data.
    console.log('yay?');
    }).fail(function (jqxhr, textStatus, error) {
    var err = textStatus + ", " + error;
    console.log("Request Failed: " + err);
    });
    });
    });
    </script>
    }



You also could custom above rule by follow the above link to replace the default page app model provider






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340528%2ffinding-it-impossible-to-post-simple-ajax%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Excuse me for posting this answer, I'd rather do this in the comment section, but I don't have the privilege yet.



    Shouldn't your PageModel look like this ?






    [HttpPost]
    public IActionResult GetPartDetails() {
    return new JsonResult {
    Text = "text", Value = "value"
    };
    }








    share|improve this answer
























    • Thanks for your answer, but it doesnt let me instantiate JsonResult like that - it just says TExt and Value dont exist

      – Bassie
      Nov 16 '18 at 15:43
















    0














    Excuse me for posting this answer, I'd rather do this in the comment section, but I don't have the privilege yet.



    Shouldn't your PageModel look like this ?






    [HttpPost]
    public IActionResult GetPartDetails() {
    return new JsonResult {
    Text = "text", Value = "value"
    };
    }








    share|improve this answer
























    • Thanks for your answer, but it doesnt let me instantiate JsonResult like that - it just says TExt and Value dont exist

      – Bassie
      Nov 16 '18 at 15:43














    0












    0








    0







    Excuse me for posting this answer, I'd rather do this in the comment section, but I don't have the privilege yet.



    Shouldn't your PageModel look like this ?






    [HttpPost]
    public IActionResult GetPartDetails() {
    return new JsonResult {
    Text = "text", Value = "value"
    };
    }








    share|improve this answer













    Excuse me for posting this answer, I'd rather do this in the comment section, but I don't have the privilege yet.



    Shouldn't your PageModel look like this ?






    [HttpPost]
    public IActionResult GetPartDetails() {
    return new JsonResult {
    Text = "text", Value = "value"
    };
    }








    [HttpPost]
    public IActionResult GetPartDetails() {
    return new JsonResult {
    Text = "text", Value = "value"
    };
    }





    [HttpPost]
    public IActionResult GetPartDetails() {
    return new JsonResult {
    Text = "text", Value = "value"
    };
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 15:36









    Sk83r1l4m4Sk83r1l4m4

    11711




    11711













    • Thanks for your answer, but it doesnt let me instantiate JsonResult like that - it just says TExt and Value dont exist

      – Bassie
      Nov 16 '18 at 15:43



















    • Thanks for your answer, but it doesnt let me instantiate JsonResult like that - it just says TExt and Value dont exist

      – Bassie
      Nov 16 '18 at 15:43

















    Thanks for your answer, but it doesnt let me instantiate JsonResult like that - it just says TExt and Value dont exist

    – Bassie
    Nov 16 '18 at 15:43





    Thanks for your answer, but it doesnt let me instantiate JsonResult like that - it just says TExt and Value dont exist

    – Bassie
    Nov 16 '18 at 15:43













    0














    Somehow I found a setup that works but I have no idea why..



    PageModel



        [HttpGet]
    public IActionResult OnGetPart(string input)
    {
    var bellNumber = input.Split('_')[1];
    var partDetail = _context.Parts.FirstOrDefault(p => p.BellNumber == bellNumber);

    return new JsonResult(partDetail);
    }


    Razor Page



    $.ajax({
    type: 'GET',
    url: "/Packing/Package/" + @Model.Package.Id,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: {
    input: barcode,
    handler: 'Part'
    },

    success: function (datas) {
    console.log('success');
    $('#partDescription').html(datas.description);
    }
    });





    share|improve this answer





















    • 1





      It works because you have stumbled on the correct approach. It's worth spending a little time researching how handler methods work (learnrazorpages.com/razor-pages/handler-methods), and how to use the jQuery ajax method (api.jquery.com/jquery.ajax). Also, there is no place for the [HttpGet] or [HttpPost] attributes in Razor Pages. They belong to MVC and have no effect at all in Razor Pages.

      – Mike Brind
      Nov 16 '18 at 16:06











    • @MikeBrind Thanks for the tip Mike - I'll check out those pages

      – Bassie
      Nov 16 '18 at 16:07













    • return new JsonResult("hello"); was most likely your problem, because "hello" is not a valid Json object.

      – Adam Vincent
      Nov 16 '18 at 16:10











    • @AdamVincent But then wouldn't the breakpoint still go to the method?

      – Bassie
      Nov 16 '18 at 16:11











    • @AdamVincent the main error was that the AJAX call was not being made to the correct page. Then in the first example, the handler method name did not follow convention and would never get selected by the framework.

      – Mike Brind
      Nov 16 '18 at 17:02
















    0














    Somehow I found a setup that works but I have no idea why..



    PageModel



        [HttpGet]
    public IActionResult OnGetPart(string input)
    {
    var bellNumber = input.Split('_')[1];
    var partDetail = _context.Parts.FirstOrDefault(p => p.BellNumber == bellNumber);

    return new JsonResult(partDetail);
    }


    Razor Page



    $.ajax({
    type: 'GET',
    url: "/Packing/Package/" + @Model.Package.Id,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: {
    input: barcode,
    handler: 'Part'
    },

    success: function (datas) {
    console.log('success');
    $('#partDescription').html(datas.description);
    }
    });





    share|improve this answer





















    • 1





      It works because you have stumbled on the correct approach. It's worth spending a little time researching how handler methods work (learnrazorpages.com/razor-pages/handler-methods), and how to use the jQuery ajax method (api.jquery.com/jquery.ajax). Also, there is no place for the [HttpGet] or [HttpPost] attributes in Razor Pages. They belong to MVC and have no effect at all in Razor Pages.

      – Mike Brind
      Nov 16 '18 at 16:06











    • @MikeBrind Thanks for the tip Mike - I'll check out those pages

      – Bassie
      Nov 16 '18 at 16:07













    • return new JsonResult("hello"); was most likely your problem, because "hello" is not a valid Json object.

      – Adam Vincent
      Nov 16 '18 at 16:10











    • @AdamVincent But then wouldn't the breakpoint still go to the method?

      – Bassie
      Nov 16 '18 at 16:11











    • @AdamVincent the main error was that the AJAX call was not being made to the correct page. Then in the first example, the handler method name did not follow convention and would never get selected by the framework.

      – Mike Brind
      Nov 16 '18 at 17:02














    0












    0








    0







    Somehow I found a setup that works but I have no idea why..



    PageModel



        [HttpGet]
    public IActionResult OnGetPart(string input)
    {
    var bellNumber = input.Split('_')[1];
    var partDetail = _context.Parts.FirstOrDefault(p => p.BellNumber == bellNumber);

    return new JsonResult(partDetail);
    }


    Razor Page



    $.ajax({
    type: 'GET',
    url: "/Packing/Package/" + @Model.Package.Id,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: {
    input: barcode,
    handler: 'Part'
    },

    success: function (datas) {
    console.log('success');
    $('#partDescription').html(datas.description);
    }
    });





    share|improve this answer















    Somehow I found a setup that works but I have no idea why..



    PageModel



        [HttpGet]
    public IActionResult OnGetPart(string input)
    {
    var bellNumber = input.Split('_')[1];
    var partDetail = _context.Parts.FirstOrDefault(p => p.BellNumber == bellNumber);

    return new JsonResult(partDetail);
    }


    Razor Page



    $.ajax({
    type: 'GET',
    url: "/Packing/Package/" + @Model.Package.Id,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: {
    input: barcode,
    handler: 'Part'
    },

    success: function (datas) {
    console.log('success');
    $('#partDescription').html(datas.description);
    }
    });






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 16 '18 at 16:07

























    answered Nov 16 '18 at 16:03









    BassieBassie

    4,0562356




    4,0562356








    • 1





      It works because you have stumbled on the correct approach. It's worth spending a little time researching how handler methods work (learnrazorpages.com/razor-pages/handler-methods), and how to use the jQuery ajax method (api.jquery.com/jquery.ajax). Also, there is no place for the [HttpGet] or [HttpPost] attributes in Razor Pages. They belong to MVC and have no effect at all in Razor Pages.

      – Mike Brind
      Nov 16 '18 at 16:06











    • @MikeBrind Thanks for the tip Mike - I'll check out those pages

      – Bassie
      Nov 16 '18 at 16:07













    • return new JsonResult("hello"); was most likely your problem, because "hello" is not a valid Json object.

      – Adam Vincent
      Nov 16 '18 at 16:10











    • @AdamVincent But then wouldn't the breakpoint still go to the method?

      – Bassie
      Nov 16 '18 at 16:11











    • @AdamVincent the main error was that the AJAX call was not being made to the correct page. Then in the first example, the handler method name did not follow convention and would never get selected by the framework.

      – Mike Brind
      Nov 16 '18 at 17:02














    • 1





      It works because you have stumbled on the correct approach. It's worth spending a little time researching how handler methods work (learnrazorpages.com/razor-pages/handler-methods), and how to use the jQuery ajax method (api.jquery.com/jquery.ajax). Also, there is no place for the [HttpGet] or [HttpPost] attributes in Razor Pages. They belong to MVC and have no effect at all in Razor Pages.

      – Mike Brind
      Nov 16 '18 at 16:06











    • @MikeBrind Thanks for the tip Mike - I'll check out those pages

      – Bassie
      Nov 16 '18 at 16:07













    • return new JsonResult("hello"); was most likely your problem, because "hello" is not a valid Json object.

      – Adam Vincent
      Nov 16 '18 at 16:10











    • @AdamVincent But then wouldn't the breakpoint still go to the method?

      – Bassie
      Nov 16 '18 at 16:11











    • @AdamVincent the main error was that the AJAX call was not being made to the correct page. Then in the first example, the handler method name did not follow convention and would never get selected by the framework.

      – Mike Brind
      Nov 16 '18 at 17:02








    1




    1





    It works because you have stumbled on the correct approach. It's worth spending a little time researching how handler methods work (learnrazorpages.com/razor-pages/handler-methods), and how to use the jQuery ajax method (api.jquery.com/jquery.ajax). Also, there is no place for the [HttpGet] or [HttpPost] attributes in Razor Pages. They belong to MVC and have no effect at all in Razor Pages.

    – Mike Brind
    Nov 16 '18 at 16:06





    It works because you have stumbled on the correct approach. It's worth spending a little time researching how handler methods work (learnrazorpages.com/razor-pages/handler-methods), and how to use the jQuery ajax method (api.jquery.com/jquery.ajax). Also, there is no place for the [HttpGet] or [HttpPost] attributes in Razor Pages. They belong to MVC and have no effect at all in Razor Pages.

    – Mike Brind
    Nov 16 '18 at 16:06













    @MikeBrind Thanks for the tip Mike - I'll check out those pages

    – Bassie
    Nov 16 '18 at 16:07







    @MikeBrind Thanks for the tip Mike - I'll check out those pages

    – Bassie
    Nov 16 '18 at 16:07















    return new JsonResult("hello"); was most likely your problem, because "hello" is not a valid Json object.

    – Adam Vincent
    Nov 16 '18 at 16:10





    return new JsonResult("hello"); was most likely your problem, because "hello" is not a valid Json object.

    – Adam Vincent
    Nov 16 '18 at 16:10













    @AdamVincent But then wouldn't the breakpoint still go to the method?

    – Bassie
    Nov 16 '18 at 16:11





    @AdamVincent But then wouldn't the breakpoint still go to the method?

    – Bassie
    Nov 16 '18 at 16:11













    @AdamVincent the main error was that the AJAX call was not being made to the correct page. Then in the first example, the handler method name did not follow convention and would never get selected by the framework.

    – Mike Brind
    Nov 16 '18 at 17:02





    @AdamVincent the main error was that the AJAX call was not being made to the correct page. Then in the first example, the handler method name did not follow convention and would never get selected by the framework.

    – Mike Brind
    Nov 16 '18 at 17:02











    0














    For this issue, it is related with the Razor page handler. For default handler routing.




    Handler methods for HTTP verbs ("unnamed" handler methods) follow a
    convention: On[Async] (appending Async is optional but
    recommended for async methods).




    For your original post, to make it work with steps below:




    • Change GetPartDetails to OnGetPartDetails which handler is PartDetails and http verb is Get.

    • Remove [HttpPost] which already define the Get verb by OnGet.


    • Client Request



       @section Scripts{ 
      <script type="text/javascript">
      $(document).ready(function(){
      $("#scanInput").change(function () {
      console.log('ye');

      $.getJSON("?handler=PartDetails", function (data) {
      //Do something with the data.
      console.log('yay?');
      }).fail(function (jqxhr, textStatus, error) {
      var err = textStatus + ", " + error;
      console.log("Request Failed: " + err);
      });
      });
      });
      </script>
      }



    You also could custom above rule by follow the above link to replace the default page app model provider






    share|improve this answer




























      0














      For this issue, it is related with the Razor page handler. For default handler routing.




      Handler methods for HTTP verbs ("unnamed" handler methods) follow a
      convention: On[Async] (appending Async is optional but
      recommended for async methods).




      For your original post, to make it work with steps below:




      • Change GetPartDetails to OnGetPartDetails which handler is PartDetails and http verb is Get.

      • Remove [HttpPost] which already define the Get verb by OnGet.


      • Client Request



         @section Scripts{ 
        <script type="text/javascript">
        $(document).ready(function(){
        $("#scanInput").change(function () {
        console.log('ye');

        $.getJSON("?handler=PartDetails", function (data) {
        //Do something with the data.
        console.log('yay?');
        }).fail(function (jqxhr, textStatus, error) {
        var err = textStatus + ", " + error;
        console.log("Request Failed: " + err);
        });
        });
        });
        </script>
        }



      You also could custom above rule by follow the above link to replace the default page app model provider






      share|improve this answer


























        0












        0








        0







        For this issue, it is related with the Razor page handler. For default handler routing.




        Handler methods for HTTP verbs ("unnamed" handler methods) follow a
        convention: On[Async] (appending Async is optional but
        recommended for async methods).




        For your original post, to make it work with steps below:




        • Change GetPartDetails to OnGetPartDetails which handler is PartDetails and http verb is Get.

        • Remove [HttpPost] which already define the Get verb by OnGet.


        • Client Request



           @section Scripts{ 
          <script type="text/javascript">
          $(document).ready(function(){
          $("#scanInput").change(function () {
          console.log('ye');

          $.getJSON("?handler=PartDetails", function (data) {
          //Do something with the data.
          console.log('yay?');
          }).fail(function (jqxhr, textStatus, error) {
          var err = textStatus + ", " + error;
          console.log("Request Failed: " + err);
          });
          });
          });
          </script>
          }



        You also could custom above rule by follow the above link to replace the default page app model provider






        share|improve this answer













        For this issue, it is related with the Razor page handler. For default handler routing.




        Handler methods for HTTP verbs ("unnamed" handler methods) follow a
        convention: On[Async] (appending Async is optional but
        recommended for async methods).




        For your original post, to make it work with steps below:




        • Change GetPartDetails to OnGetPartDetails which handler is PartDetails and http verb is Get.

        • Remove [HttpPost] which already define the Get verb by OnGet.


        • Client Request



           @section Scripts{ 
          <script type="text/javascript">
          $(document).ready(function(){
          $("#scanInput").change(function () {
          console.log('ye');

          $.getJSON("?handler=PartDetails", function (data) {
          //Do something with the data.
          console.log('yay?');
          }).fail(function (jqxhr, textStatus, error) {
          var err = textStatus + ", " + error;
          console.log("Request Failed: " + err);
          });
          });
          });
          </script>
          }



        You also could custom above rule by follow the above link to replace the default page app model provider







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 4:18









        Tao ZhouTao Zhou

        7,58231434




        7,58231434






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340528%2ffinding-it-impossible-to-post-simple-ajax%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python