Problem while using AJAX requesting webmethod info from VB file - javascript

I've been trying to call a VB.NET function from JavaScript for the website I've been working on, and found out about AJAX and started trying to use it.
(For reference, my target framework is 4.6.1.)
The following is a snippet of my VB.NET code, where PopulateDetailSection() is something which returns a string which is supposed to be the text from the div, but my breakpoint never hits this function.
System.Web.Services.WebMethod(EnableSession:=True, BufferResponse:=False)
Protected Shared Function PopulateDetail() As HtmlGenericControl
Return PopulateDetailSection()
End Function
and as for the AJAX call:
jQuery.ajax({
url: "frmActiveTrackingG.aspx/PopulateDetail",
type: "GET",
//contentType: "application/json: charset=utf-8",
dataType:"html",
success: function (data) {
alert(data);
}
});
I've tried alerting several things, but it keeps returning undefined unless I alert data which appears to return the aspx file starting with the header.
I usually don't ask questions here, but I'm truly stumped on this.

There some issues with your JavaScript. As described here: https://stackoverflow.com/a/5332290/428010 you need to post on the web page/method.
jQuery.ajax({
url: "frmActiveTrackingG.aspx/PopulateDetail",
type: "POST",
contentType: "application/json: charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
}
});
On the VB side the method need to be public not protected.

Related

Ajax not working with a specific file name

Something really weird happened today and I cannot understand its behavior so if someone can help me to understand I would really appreciate it,
So I tried to do ajax call like below
var dto = {
'entity': 'SOME_VAL'
};
$.ajax({
type: 'GET',
url: '/WebService/checkout/applyCoupon.php',
dataType: "json",
data: dto,
contentType: "application/json; charset=utf-8",
cache: false,
success: function (response) {
console.log('Success!');
},
error: function() {
console.log('Error!');
}
});
and it returned my website's HTML code instead of JSON return values.
After a while I was able to resolve the issue by using the same code with diffrent file names.
url: '/WebService/checkout/applyCoupon.php', //NOT WORKING
url: '/WebService/checkout/applyCoupon2.php', //NOT WORKING
url: '/WebService/checkout/coupon.php', //NOT WORKING
url: '/WebService/checkout/apply.php', //WORKING
url: '/WebService/checkout/ap.php', //WORKING
It seems like it does not work when the file name contains coupon in it.
I have other websites running just fine using the exact same code with the file name applyCoupon.php so I do not understand why it won't work on a specific website. I can just simply use a different file name but I want to understand how/why it could happen. The environment is IIS. Can someone please help me to understand this issue? Thank you!

Getting POST API to work with ajax using JavaScript

I'm trying to reach an API from inside my tag manager. The language to use for the so-called building blocks in this tag-manager is JavaScript. The POST API I am trying to use is fully functioning and works like this:
input:
{"features": "xyz"}
output:
[0.45]
I set up an ajax statement, but it does not seem to work.
I have tried all different kinds of input and content types. The code looks like this:
$.ajax({
url: apiurl,
type: "POST",
data: {"features": "xyz"},
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data)
},
error: function(){
console.log("Error"); }
});
What I am doing wrong here? I keep getting error messages.

Accessing PHP variable as URL in JQuery Ajax request

I am experiencing some problems with having the Ajax request function/method in JQuery recognizing a PHP-variable from outside the script code. What I am trying to do is using the variable $live_update_url as the url-argument in the Ajax code. The code below is not working, but if I hard code the value of the url there are no problems. So it should be the variable itself that is not accessed. What am I doing wrong here?
function ajaxd() {
$.ajax({
url: <?php print($live_update_url);?>,
type: "get",
data: {live_time: 'value'},
dataType: "json",
success: function(data){
$('#local_time').html(data.live_time);
}
});
It looks like you are missing the quotes around the URL value in your JSON.
Make sure that the value returned by the $live_update_url includes quotes or try this:
function ajaxd() {
$.ajax({
url: "<?php print($live_update_url);?>",
type: "get",
data: {live_time: 'value'},
dataType: "json",
success: function(data){
$('#local_time').html(data.live_time);
}
});
Found a solution:
Used the following definition of $live_update_url in the PHP code:
$live_update_url = "'http://localhost/projectName/api/time.php?g_id=".$g_id."'";
This string already includes single quotes within the double quotes. Then I used the echo <<<_END-construct and just added the following line in the Ajax request:
url: $live_update_url

WebService in JqueryMobile(Response in Json)

I'm doing a project in Jquery Mobiles and need an webservice. Normally in android we have the http connection and we got the response as xml/json etc.. We parse the data and processed to the view. Here,In case of jquery , facing problem to solve the issue.. I read that ajax call is using for the webservices.
Sample code
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "some url",
data: "{}",
dataType: "json",
success: function (msg) {
alert(msg);
//$("#resultLog").html(msg);
}
});
Also,I refer the google api_Place Search, the service for this is entirely different
RefernceLink Click HERE!
Does anyone suggest the proper way to implement the webservice to interact with my server and retrieve the json response?
Thanks in Advance!

Send request to server side on the text using asp.net

I want to send request on server side onFocus event but my method is not calling, really confused, tried jquery ajax for that but all failed is there any other possible way to send request on server side using asp.net textbox onFocus event?
asp.net website project is created for work.
Ajax method code
$.ajax({
type: "POST",
url: "About.aspx/GetRes",
data: "{}",
contentType: "application/text; charset=utf-8",
dataType: "text",
success: function (msg) {
alert(msg);
}
});
Method which is calling
Public Function GetRes() As String
Return "I am calling GetRes method"
End Function
If you are able to access the supplied url via browser properly then please try this..
$.ajax({
type: "POST",
url: "About.aspx/GetRes",
dataType: "text",
success: function (msg) {
alert(msg);
}
});
Also, i'm considering that your event biding is wrapped under the document.ready callback
and make the server side method like this
<WebMethod>
Public Shared Function GetRes() As String
Return "I am calling GetRes method"
End Function
You can only call PageMethods like that.
Such methods should be static(Shared) and have the WebMethod attribute.
<WebMethod()>
Public Shared Function GetRes() As String
Return "I am calling GetRes method"
End Function

Categories