How to fetch wcf dataservice by jquery? - asp.net

I am trying to fetch wcf service from jquery. I have written below code but getting error.
jquery code:
function LoadAutoComplete(method, param) {
firstIncrement = pageSize + 1;
$.ajax({
type: "POST",
url: "Services/Popup.svc/" + method,
dataType: "json",
contentType: "application/json; charset=utf-8",
data: params,
processdata: true,
success: function (data) {
if (data.PopupData != null) {
alert(data);
}
else {
//response = null;
//return response;
}
},
error: function (result) { alert("Error"); }
});
}
function LoadTest(id) {
var prm = "<%= this.Sequence %>";
var params = '"sequence":' + prm;
method = "GetNotes";
LoadAutoComplete(method,params);
}
.aspx page
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="JS/Popup.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
LoadAutoComplete("GetNotes","<%= this.Sequence %>");
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
GetNotes is my method name which i have written in Business logic.
Error: Microsoft JScript runtime error: '$' is undefined

You need to include jQuery, add the following script tag above the others:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

Related

How to get refresh token from Google Sign-In?

I created a prototype to ask the permission from User for accessing their Google Calendar by using ASP.NET WebForms.
Here is my code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GoogleSignInDemo.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="CLIENT_ID.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="Resources/jquery.js" ></script>
</head>
<body>
<div class="g-signin2" data-theme="dark" onclick="onSignInCalendar()"></div>
<script>
function onSignInCalendar() {
var auth2 = gapi.auth2.getAuthInstance();
var options = new gapi.auth2.SigninOptionsBuilder(
{ 'scope': 'email https://www.googleapis.com/auth/calendar' });
googleUser = auth2.currentUser.get();
googleUser.grant(options).then(
function (success) {
console.log(JSON.stringify({ message: "success", value: success }));
var accessToken = success.Zi.access_token;
var idToken = success.Zi.id_token;
},
function (fail) {
alert(JSON.stringify({ message: "fail", value: fail }));
});
}
</script>
</body>
</html>
It works fine. However, the response from google only contain access_token and id_token. I can't find refresh_token in there.
Any knows how to get it?

Ajax Not returning Data to Action ASP.net MVC

I am new to MVC and facing a problem with accessing data via ajax in action of my ASP.NET Application.
Here is my Ajax Code:
$('.testBtn').click(function() {
$.ajax({
type: 'GET',
data: { id: 10 },
url="#Url.Action("GetData", "Consultation")",
success:function()
{
}
});
});
And here is my action inside the controller:
public ActionResult GetData(int id)
{
string x=id.ToStrring();
return null;
}
For testing I am just passing a static integer and getting its value inside my action.
On clicking the button I am getting the following error:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult GetData(Int32)' in 'careshade_mvc.Controllers.ConsultationController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Here is working code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.testBtn').click(function () {
$.ajax(
{
url: 'Consultation/GetData',
type: "POST",
data: { id: 10 },
success: function (result) {
}
});
});
});
</script>
</head>
<body>
<input type="button" class="testBtn" value="ClickHere" name="button">
</body>
</html>
Try this:
$(document).ready(function() {
var id = 10;
$('.testBtn').click(function () {
$.ajax(
{
url: 'Consultation/GetData/' + id,//instead data: { id: 10 },
type: "GET",
success: function (result) {
}
});
});
});
You need to use a colon(:) character after url not the equals(=) sign.
Simply change:
url="#Url.Action("GetData", "Consultation")",
To this:
url:"#Url.Action("GetData", "Consultation")",
This was the only change I had to make to make the call work.Hope it helps you!

Kendo Grid not binding to JSON result from ASMX Web Service

I have a problem binding Kendo GRID to ASP.NET asmx web service.
Following is HTML Code
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="styles/kendo.common.min.css" />
<link rel="stylesheet" href="styles/kendo.default.min.css" />
<link rel="stylesheet" href="styles/kendo.dataviz.min.css" />
<link rel="stylesheet" href="styles/kendo.dataviz.default.min.css" />
<link href="styles/kendo.common-bootstrap.min.css" rel="stylesheet" />
<link href="styles/kendo.bootstrap.min.css" rel="stylesheet" />
<link href="../BOOTSTRAP/bootstrap.min.css" rel="stylesheet" />
<script src="../Scripts/jquery-2.0.3.min.js"></script>
<script src="js/kendo.all.min.js"></script>
<script src="../BOOTSTRAP/bootstrap.min.js"></script>
</head>
<body>
<div id="example">
<div id="grid" class="table table-bordered"></div>
<script>
$(document).ready(function () {
$("#grid").kendoGrid({
columns: [
{ field: "srno", title: "SRNO" },
{ field: "party", title: "PARTY" }
],
dataSource: new kendo.data.DataSource({
transport: {
read: {
url: "../Service/DatabaseHandling.asmx/GetPurchaseJangad_JSON",
dataType: "json",
contentType: "application/json; charset=utf-8"
}
}
}),
schema: {
data: "d"
},
sortable: true
});
});
</script>
</div>
</body>
</html>
JSON returned from service is looking ok as below.
{"d":"[{\"srno\":17,\"party\":\"PARESH\",\"dt\":\"11/5/2015\",\"weight\":15000.0,\"timestamp\":\"2015-05-11T20:19:55.093\"},{\"srno\":18,\"party\":\"SIM\",\"dt\":\"11/5/2015\",\"weight\":11000.0,\"timestamp\":\"2015-05-11T20:21:44.177\"}]"}
Still nothing is visible on GRID.
And no error in browser console.
I think the issue is with the JSON data only.
Modified JSON given below, replaced \" with " and removed the quote just before and after the square brackets
{"d":[{"srno":17,"party":"PARESH","dt":"11/5/2015","weight":15000.0,"timestamp":"2015-05-11T20:19:55.093"},{"srno":18,"party":"SIM","dt":"11/5/2015","weight":11000.0,"timestamp":"2015-05-11T20:21:44.177"}]}
Please find the fiddle here after cleaning up the json
I changed the jQuery code to following.
$(document).ready(function () {
var data;
var webMethod = "../Service/DatabaseHandling.asmx/GetPurchaseJangad_JSON";
var parameters = "{}";
$.ajax({
contentType: "application/json; charset=utf-8",
url: webMethod,
data: parameters,
dataType: "json",
success: function (response) {
data = $.parseJSON(response.d);
console.log(data);
$("#grid").kendoGrid({
columns: [
{ field: "srno", title: "SRNO" },
{ field: "party", title: "PARTY" },
{ field: "dt", title: "DATE" },
{ field: "weight", title: "WEIGHT" }
],
dataSource: {
transport: {
read: function (options) {
options.success(data);
}
},
schema: {
}
}
});
}
});
});
What I did, is called that service via jQuery ajax. Got the data containing backslash and quotes. Then as in code, used $.parseJSON to get the required JSON format. Then passed that data to Kendo GRID after success. And thats how its working for me.
But still I am looking for a way to do this on server side, if possible.
Thanks

Ajax request with web method

Can someone explain me why this gives me an error?
My ajax call something like this.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function () {
$('#btn1').click(function () {
var values = JSON.stringify({ data: $('#form1').serializeArray() });
alert($('#form1').serializeArray());
$.ajax({
type: "POST",
url: "Default.aspx/Test",
contentType: "application/json; charset=utf-8",
scripts: true,
dataType: "json",
data: values,
success: function (data) { $('#results').append(data.d); },
error: function () { $('#results').append('hata'); }
});
}); });
</script>
</head>
<body>
<form runat="server" id="form1">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
<button id="btn1" type="button">bummm</button>
<div id="results"></div>
</form>
</body>
</html>
[WebMethod]
public static string Test (string data)
{
return "İşlem başarılı"+data;
}
It says me {"Message":"Type \u0027System.String\u0027 is not supported for deserialization of an array.","StackTrace":"
I think this happens because you wrong call your webmethod with ajax.
Your webmethod have one parameter named data with type string, but you try send without name, so try change your code like this:
var KaydetDataWithAjax = function (e)
{
var values =JSON.stringify({data: $(e).serializeArray()});
alert(values);
$.ajax({
type: "POST",
dataType: 'json',
contentType: "application/json; charset=utf-8",
url: "Harita.aspx/HaritaKaydet",
scripts: true,
data:values,
success: function (dt) { alert(dt);},
complete:function(){},
error: function () { alert('error'); }
});
};
UPDATE
this method work on new project
$.ajax({
type: "POST",
dataType: 'json',
contentType: "application/json; charset=utf-8",
url: "Harita.aspx/HaritaKaydet",
scripts: true,
data:JSON.stringify({data: 'text'}),
success: function (dt) { alert(dt);},
complete:function(){},
error: function () { alert('error'); }
});
if in your case it not work then possibly helps if you provide little more code
UPDATE 2
turns out it's all simple than i thought!
serializeArray() returns Array! So it find on server method with parameters something like List<object>, so to solve you must stringify array too
so try this code
var KaydetDataWithAjax = function (e)
{
var values =JSON.stringify({data: JSON.stringify($(e).serializeArray())});
alert(values);
$.ajax({
type: "POST",
dataType: 'json',
contentType: "application/json; charset=utf-8",
url: "Harita.aspx/HaritaKaydet",
scripts: true,
data:values,
success: function (dt) { alert(dt);},
complete:function(){},
error: function () { alert('error'); }
});
};

Asp.Net(C#) Jquery Ajax with WebMethod Call Problem

Code Behind:
[WebMethod]
public static string emp()
{
return "BlaBla";
}
Aspx Page:
$(document).ready(function() {
$.get("TestPage.aspx/emp", null, function(data) {
alert(data);
})
})
Message Box Output: TestPage.aspx on the page codes
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<style>
tr
{
background-color: red;
color: White;
}
</style>
How to make return string ?
Thank You.
use
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
$.ajax({
type: "POST",
url: ""TestPage.aspx/emp",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
In your page use
<div id="Result">Click here to return the string</div>

Resources