Subscribe to specific event in ASP.NET WebHooks - asp.net

I'm trying to learn ASP.NET WebHooks, but the docs are pretty sparse right now.
What I'm trying to do is subscribe to a specific events. All the samples I can find demonstrate subscribing to all the events, which is not very useful for me.
EDIT:
This is the code for subscribing I found in the docs:
function subscribe() {
$.ajax({
type: "POST",
url: "/api/webhooks/registrations",
data: JSON.stringify({
WebHookUri: "http://localhost:59927/api/webhooks/incoming/custom",
Secret: "12345678901234567890123456789012",
Description: "My first WebHook!"
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data, status) { alert(status); },
failure: function(errMsg) { alert(errMsg); }
});
return false;
}
What should be the code for subscribing to a "BookAdded" event?
Thanks!

So, for anyone else looking for the answer, this is how it should be done:
function subscribe() {
$.ajax({
type: "POST",
url: "/api/webhooks/registrations",
data: JSON.stringify({
WebHookUri: "http://localhost:59927/api/webhooks/incoming/custom",
Secret: "12345678901234567890123456789012",
Description: "My first WebHook!",
Filters: ["BookAdded"]
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data, status) { alert(status); },
failure: function(errMsg) { alert(errMsg); }
});
return false;
}
Note the Filters field added to the ajax statement.

Related

Microsoft AJAX vs jQuery AJAX

When calling an ASMX service, what benefits (if any) does this:
ProjectName.Services.ServiceName.MethodName('methodParameter', function (result) {
// do something with result
});
have over this:
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
//
},
error: function(e){
//
}
});
(Please note I'm not interested in "just use WCF, ASMX is outdated", it's purely for AJAX so I'm happy to stick with it for now.)

jquery autocomplete with typewatch

I am a jQuery newbie and trying to implement the autocomplete functionality along with jQuery typewatch. That is, to get the data from web service after a certain time period say 750 ms rather than after minLength.
<script type="text/javascript">
$(document).ready(function () {
$('.searchinput').autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Services/SampleWebService.asmx/GetProduct",
data: '{"searchString":"' + request.term + '"}',
dataType: "json",
async: true,
success: function (data) {
response(data);
}
});
},
});
$('.searchinput').typewatch({
callback: $.autocomplete,
wait: 750,
highlight: false
});
});
My autocomplete thing works absolutely fine but somehow I am not able to include the typewatch thing to it. I am sure there is a serious coding failure which I am not aware of.
Thanks
The jquery autocomplete have this option as parameter called delay:
http://api.jqueryui.com/autocomplete/#option-delay
So what you have to do is to change that parameter, and remove the typewatch as:
$(document).ready(function () {
$('.searchinput').autocomplete({
delay:750,
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Services/SampleWebService.asmx/GetProduct",
data: '{"searchString":"' + request.term + '"}',
dataType: "json",
async: true,
success: function (data) {
response(data);
}
});
},
});
});
The parameter called "delay" is the time you have to wait affter keydown to call external service. That is only for don't overload service with no "sensitive" search data.
What I understood from user question, is that he wants to call the autocomplete "update" function every 750ms.
You could do that changing the typewatch "segment" with this:
$('.searchinput').typewatch({
callback: function(){
$(".searchinput").data("autocomplete").search();
},
wait: 750,
highlight: false
});
That will trigger the search for the service and display popup response every 750ms

ASP.NET hello world AJAX post

I keep getting a 500 with the following C#/jQuery. Any given implementation may not be right and thats not a huge issue. I'm just trying to get a hello world up. It works if the c# has no arguments but as soon as I try to recieve data it gives the 500.
[WebMethod]
public static string Test(string s)
{
// never gets here
}
$.ajax({
type: "POST",
url: "ajax.aspx/" + method,
/*async: true,*/
data: "{data:'" + data + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
callback(data.d);
}
});
latest attempt is this which still doesnt work:
[WebMethod()]
public static string Test(string data)
{
// never gets here
return "hello world";
}
$.ajax({
type: "POST",
url: "ajax.aspx/Test",
data: "data: {data:'abc'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("back");
}
});
I think you don't have to use MVC to make it work. I think the way you are passing json parameters are wrong. Please check below code and try and do let me know whether it works.
[WebMethod()]
public static string Test(string data)
{
// never gets here
return "hello world";
}
$.ajax({
type: "POST",
url: "ajax.aspx/Test",
data:'{"data":"abc"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});
try this
[HttpPost]
public ActionResult Test(string x)
{
// never gets here
return Json(true)
}
$.ajax({
type: "Post",
url: "ajax/Test",
data: {x:'abc'},
dataType: "json",
success: function (data) {
alert("back");
}
});

How to read Model values and send them by ajax post

I have the following code and it`s pointing out errors as follows
Error 1 The name 'date' does not exist in the current context
Error 2 The name 'person' does not exist in the current context
What is wrong?
$("#test").Click(function () {
var date = $("#DateFrom").val();
var person = Model.SelectedPerson;
$.ajax({
url: '#Url.Action("testEmp","Employee",new {dateFrom = date, selectedPerson= person})',
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (result) {
$('#text).html(result);
},
});
return false;
});
Try this:
$.ajax({
url: '#Url.Action("ActionName")',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ date: '..', person: '...' }),
success: function(result) {
}
});
EDIT: Take a look at this answer for the complete solution.
jquery ajax forms for ASP.NET MVC 3

calling asmx service using jquery ajax asp.net 4.0

I'm trying to call a sample asmx service using jquery, here is the jquery code
$.ajax({
type: "POST",
url: "/Services/Tasks.asmx/HelloWorld",
data: "{}",
dataType: "json",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);
}
});
This is not showing any message,code is in asp.net 4.0,
Am I missing any thing?
Edit - I changed the dataType to xml, now success function is working it return following xml
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
I'm using following code to parse xml data and it is showing null in alert
success: function (data) {
edata = $(data).find("string").html();
alert(data);
}
I believe it's because you have the dataType: "json" and it's expecting the response content-type to be the same but XML is being returned. I bet the complete event is being raised but not success.
try
$.ajax({
type: "POST",
url: "/Services/Tasks.asmx/HelloWorld",
data: "{}",
dataType: "json",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);
},
complete: function (data) {
alert(data);
}
});
UPDATE
I think it's because you're using .html(), you need to use text(). Also i don't know if you meant to do it or not but you have data in your alert, i'm assuming you meant to use edata. The following worked for me:
jQuery.ajax({
type: "POST",
url: "/yourURL",
dataType: "xml",
data: "{}",
contentType: "application/xml; charset=utf-8",
success: function(data) {
edata = $(data).find("string").text();
alert(edata);
}
})
I'd recommend adding the [ScriptService] attribute to your Tasks.asmx class so it will accept and respond in JSON instead of XML. Your client code looks good, but you'll want to take a look at "data.d" instead of "data" in your success handler.
use it.
<script>
alert("aaa");
$.ajax({
type: "POST",
url: "MyService.asmx/HelloWorld",
data: "{}",
dataType: "xml",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);//data-object xmldocument
edata = $(data).children("string").text();
alert(edata);
}
});
alert("bbb");
</script>
Well, you're stating that the dataType is JSON, but the contentType is XML. Try
contentType: "application/json; charset=utf-8",
If not, then we'd have to see the asmx code.

Resources