RyeGuy RyeGuy - 3 years ago 223
Ajax Question

Post to azure function - Error parsing boolean value

On my site I make a post request using a ajax call to an azure function url like so:

var name = $("input#name").val();
var number = $(input#number").val();

$.ajax({
url: "azurefunctionurl.com",
type: "POST",
data: {
name: name,
number: number,
},
cache: false,
success: function() {
...stuff here....
},
error: function () {
...stuff here...
},
});


However in the error logs I see a message that says


One or more errors occurred. ---> Error parsing boolean value. Path '', line 1, position 1.


My azure function code looks like

#r "Newtonsoft.Json"

using System;
using System.Net;
using Newtonsoft.Json;

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
log.Verbose($"Webhook was triggered!");

string jsonContent = await req.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(jsonContent);

string name = data.name;
string number = data.number;

log.Verbose(name);
}


What am I doing wrong?

Answer Source

because when you post by ajax with default content type, the form data will be

name=xxxx&number=yyyy

so whether you need to change your azure function codes to recognize the name value pair, or you need to set ajax post content type to

application/json
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download