I have been trying to get a simple example of the jquery-ui autocomplete to work. I have a controller setup to handle the query, and it returns the string that looks to be in order, but I am getting no suggestions showing up.
Here are the libraries I am including on that page
<link href="/Content/css/jquery-ui-1.8.12.custom.css" rel="stylesheet" type="text/css" />
<link href="/Content/css/jquery.fancybox-1.3.4.css" rel="stylesheet" type="text/css" />
<link href="/Content/css/Stylesheet.css" rel="stylesheet" type="text/css" />
<link href="/Content/css/coupon1.css" rel="stylesheet" type="text/css" />
<link href="/Content/Slider/slidder.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui-1.8.23.custom.min.js" type="text/javascript"></script>
<script src="/Scripts/el7r_notify.min.jq.js" type="text/javascript"></script>
<script src="/Scripts/ZeroClipboard.js" type="text/javascript"></script>
<script src="/Scripts/jquery.spellcheck.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.fancybox-1.3.4.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="/Content/Slider/jquery.jcarousel.min.js" type="text/javascript"></script>
<script src="/Content/Slider/jquery-func.js" type="text/javascript"></script>
<script src="/Scripts/Coupon.js" type="text/javascript"></script>
and here is the javascript and the form tags:
$(document).ready(function () {
$("#tags").autocomplete({
source: function (request, response) {
$.ajax({
url: '/Home/GetCompanyNames',
success: function (msg) {
response($.map(msg));
}
});
}
});
});
<div class="ui-widget">
<label for="tags">tags: </label>
<input id="tags" />
</div>
I get back a string response that looks reasonable from my controller:
"LTJRKK,
KTOYQQ,
GDADKT,
PVFOQT,
PVFOQT,
YNKYVS,
YNKYVS,
DQBOVU,
DQBOVU
"
this string seem to be the default naming that autocomplete is looking for.
But I get no joy at all. Any thoughts?
Your call to $.map is incorrect. You are not supplying the callback function to $.map, which is probably causing the problem.
In this case, you do not need to use $.map at all since your action returns data in the correct format. The following should work fine:
$(document).ready(function () {
$("#tags").autocomplete({
source: '/Home/GetCompanyNames'
});
});
Probably you need this:
$("#tags").autocomplete({
source: function(request, response) {
$.ajax({
url: "url",
data: request,
dataType: "json",
method: "post",
success: response
}
}
});
Related
I installed LazyLoading jquery plugin and try to use it in my bootstrap mvc project. In examples that I saw, address of just one image is set to tag. I don't know how does it work.
How can I show all images by scrolling page. This is my Code.
<script src="~/Scripts/jquery-2.2.3.min.js"></script>
<script src="~/scripts/js/modernizr.custom.js"></script>
<script src="~/Scripts/jquery.lazyload.js"></script>
<script src="~/Scripts/js/bootstrap.min.js"></script>
<!--Custom-Theme-files-->
<!--theme-style-->
<link href="~/Content/css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
<link href="~/Content/css/style.css" rel="stylesheet" type="text/css" media="all" />
<link href="~/Content/css/products.css" rel="stylesheet" type="text/css" media="all" />
<div class="LazyImage">
<div class="container">
<div>
<div class="col-md-9 ">
<div>
<div class="col-md-4 product-left p-left">
<div class="product-main simpleCart_shelfItem">
<img class="lazy" src="~/Content/images/grey.gif" data-original="../Content/images/LazyLoad/example.jpg" width="200" height="200" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script language="javascript" type="text/javascript" charset="utf-8">
$(function () {
$("img.lazy").lazyload({
//threshold = 100,
//event = "sporty",
placeholder : "/Content/images/grey.gif",
event : "lazyload",
effect : "fadeIn",
effectspeed : 2000
});
});
$(window).bind("load", function () {
var timeout = setTimeout(function () {
$("img.lazy").trigger("lazyload")
}, 6000);
});
$(window).load(function () {
$("html,body").trigger("scroll");
});
</script>
I'm trying to display java code in html page, It's not working, it shows this error
any help will be appreciated. my code look like
<script type="text/javascript" src="syntaxhighlighter/scripts/shCore.js"></script>
<link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shCore.css" />
<script type="text/javascript" src="syntaxhighlighter/scripts/shBrushJava.js"></script>
<link type="text/css" rel="stylesheet" href="syntaxhighlighter/styles/shCoreEclipse.css" />
<h3>Java Code:</h3>
<pre class="brush: java;">
public class JavaClass {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
</pre>
<!-- Highlight all -->
<script type="text/javascript">
SyntaxHighlighter.all();
</script>
when i comment the following line
<script type="text/javascript" src="syntaxhighlighter/scripts/shCore.js"></script>
the above error will not appear but shows diffrent error.
I want to add basic authorization in swagger-ui, to test my WS with "Try it out" button. I tried from swagger documentation, and this topic Adding Basic Authorization for Swagger-UI, but no one work. No header is added in request, but in curl command, the basic authorization header is present. If I copy paste this curl command, all is ok.
My swagger-ui version is 2.1.2. I downloaded from github, ant put directly dist folder in static folder of my server.
Here my index.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sensor API documentation</title>
<link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16" />
<link href='css/typography.css' media='screen' rel='stylesheet' type='text/css' />
<link href='css/reset.css' media='screen' rel='stylesheet' type='text/css' />
<link href='css/screen.css' media='screen' rel='stylesheet' type='text/css' />
<link href='css/reset.css' media='print' rel='stylesheet' type='text/css' />
<link href='css/print.css' media='print' rel='stylesheet' type='text/css' />
<script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script>
<script src='lib/jquery.slideto.min.js' type='text/javascript'></script>
<script src='lib/jquery.wiggle.min.js' type='text/javascript'></script>
<script src='lib/jquery.ba-bbq.min.js' type='text/javascript'></script>
<script src='lib/handlebars-2.0.0.js' type='text/javascript'></script>
<script src='lib/underscore-min.js' type='text/javascript'></script>
<script src='lib/backbone-min.js' type='text/javascript'></script>
<script src='swagger-ui.js' type='text/javascript'></script>
<script src='lib/highlight.7.3.pack.js' type='text/javascript'></script>
<script src='lib/marked.js' type='text/javascript'></script>
<script src='lib/swagger-oauth.js' type='text/javascript'></script>
<!-- Some basic translations -->
<!-- <script src='lang/translator.js' type='text/javascript'></script> -->
<!-- <script src='lang/ru.js' type='text/javascript'></script> -->
<!-- <script src='lang/en.js' type='text/javascript'></script> -->
<script type="text/javascript">
$(function() {
// Pre load translate...
if (window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
window.swaggerUi = new SwaggerUi({
url : "/swagger/json/SensorAPI.json",
validatorUrl : "",
dom_id : "swagger-ui-container",
supportedSubmitMethods : [ 'get', 'post', 'put', 'delete', 'patch' ],
onComplete : function(swaggerApi, swaggerUi) {
if (window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
$('pre code').each(function(i, e) {
hljs.highlightBlock(e)
});
addAuthorization();
},
onFailure : function(data) {
log("Unable to Load SwaggerUI");
},
docExpansion : "none",
apisSorter : "alpha",
showRequestHeaders : false
});
function addAuthorization() {
var username = $('#input_username').val();
var password = $('#input_password').val();
if (username && username.trim() != "" && password && password.trim() != "") {
var basicAuth = new SwaggerClient.PasswordAuthorization(username, password);
window.swaggerUi.api.clientAuthorizations.add('Basic', basicAuth);
log("authorization added: username = " + username + ", password = " + password);
} else {
window.authorizations.remove();
}
}
$('#input_username').change(addAuthorization);
$('#input_password').change(addAuthorization);
// pre-populate on the page using demo account
$('#input_username').val("demo");
$('#input_password').val("demo");
window.swaggerUi.load();
function log() {
if ('console' in window) {
console.log.apply(console, arguments);
}
}
});
</script>
</head>
<body class="swagger-section">
<div id='header'>
<div class="swagger-ui-wrap">
<a id="logo" href="http://swagger.io">swagger</a>
<form id='api_selector'>
<div class='input'>
Username: <input placeholder="username" id="input_username" name="username" type="text" size="10" />
</div>
<div class='input'>
Password: <input placeholder="password" id="input_password" name="password" type="password" size="10" />
</div>
</form>
</div>
</div>
<div id="message-bar" class="swagger-ui-wrap" data-sw-translate> </div>
<div id="swagger-ui-container" class="swagger-ui-wrap"></div>
</body>
</html>
Please tell me what is wrong. Thanks
I found : the issue came from my json files. I defined many "securityDefinitions", one per user role
...,
"securityDefinitions":{
"administrator":{
"type":"basic",
"description":"The administrator."
},
"supervisor":{
"type":"basic",
"description":"A supervisor."
},
"customer":{
"type":"basic",
"description":"A customer."
}
},
...
and for each WS, I indicate which roles are authorized. By example :
...,
"get":{
"security":[
{
"administrator":[
]
},
{
"supervisor":[
]
}
],
...
},
...
By removing security section in WS, all is working.
In swagger editor, we need security section to test WS, but in swagger ui, this section produce an error :-(
I try to use the jQuery Globalization plugin in order to fix the comma problem with jquery unobstructive client validation. However I tried many many solutions and there no good solution to fix this. I am on a non-English localization computer and this is important that my customers enter a decimal value like "123,66" and not "123.66". ASP.NET validation tell me that the price must be a number! meh ? are you serious ? lol
I am getting this javascript error when I try to do the fix.
$.global is undefined
Here my code.
Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<title>#ViewBag.Title</title>
<link href="#Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="#Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
<script type="text/javascript" src="#Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"> </script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/cultures/globalize.cultures.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.form.js")"type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/glob.fix.js")" type="text/javascript"></script>
</head>
<body>
#RenderBody()
</body>
</html>
glob.fix.js
$.validator.methods.range = function (value, element, param) {
var globalizedValue = value.replace(",", ".");
return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}
$.validator.methods.number = function (value, element) {
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}
I can't understand.. it should work since I added ~/Scripts/globalize.js.
Any idea? or you might have a better solution for having client validation work and lets me enter comma as decimal values?
I found the way to finally get rid of the decimal problem with comma seperator!
Here a picture of the result! No more validation problems.
The steps to the fix.
1- Get the Globalization library for jQuery
You must get the latest script! Also I found some answers out there that was outdated. The object to call the library is no more $.global or anything like that but Globalize.
2- Include the scripts in your project. You must add them after jquery.validation stuff.
<script src="#Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/cultures/globalize.cultures.js")" type="text/javascript"></script>
3- Replace some methods of the validator. This will override the methods for 'number' and 'range' validation which was causing problems.
$.validator.methods.number = function (value, element) {
return this.optional(element) || !isNaN(Globalize.parseFloat(value));
}
$.validator.methods.range = function (value, element, param) {
return this.optional(element) || (Globalize.parseFloat(value) >= param[0] && Globalize.parseFloat(value) <= param[1]);
}
Globalize.parseFloat => This will actually replace anything that contains ',' to '.' if you selected a culture that require it.
After this.. You must add. This will make the globalize functions to work with the culture.
$(document).ready(function () {
Globalize.culture('fr-CA');
// Only there to show which culture are being used.
console.log(Globalize.culture().name);
});
The complete code look like...
<!DOCTYPE html>
<html>
<head>
<title>#ViewBag.Title</title>
<link href="#Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="#Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"> </script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/globalize.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/cultures/globalize.cultures.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.form.js")"type="text/javascript"></script>
<script type="text/javascript">
$.validator.methods.number = function (value, element) {
return this.optional(element) || !isNaN(Globalize.parseFloat(value));
}
$.validator.methods.range = function (value, element, param) {
return this.optional(element) || (Globalize.parseFloat(value) >= param[0] && Globalize.parseFloat(value) <= param[1]);
}
$(document).ready(function () {
Globalize.culture('fr-CA');
// Only there to show which culture are being used.
console.log(Globalize.culture().name);
});
</script>
</head>
<body>
#RenderBody()
</body>
</html>
Well, to solve the same problem, I did:
$.validator.addMethod("price",function(value){
return /^(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
});
and then used the added method as rule:
$("#form").validate({
rules: {
price: "price"
}
});
I "borrowed" the validator regex from the validation plugin itself, and inverted the dots . and the commas , (thousand separator X decimal separator).
For whatever reason I had to change my globalize reference from:
<script src="#Url.Content("~/Scripts/globalize/globalize.js")" type="text/javascript"</script>
to
<script type="text/javascript" src="~/Scripts/globalize/globalize.js"</script>
And that solved my problem. Went crazy for 30min until I got it to work. If anybody can explain why I will appreaciate.
I am developing some ajax stuff on asp.net mvc framework beta.
but,I got exception as following.
Anyone has problem like me?
Sys.ArgumentUndefinedException: Value cannot be undefined.
and my source code is like this.
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<script src="../../Scripts/jquery-1.2.6.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script type="text/javascript">
var myView;
$(pageLoad);
function pageLoad() {
myView = $create(Sys.UI.DataView, {}, {}, {}, $get("ajaxResult"));
$("#callAjaxButton").click(callActionMethod);
}
function callActionMethod() {
$.getJSON("/Home/GetCategories", bindData);
}
function bindData(data) {
myView.set_data(data);
}
</script>
<input type="button" id="callAjaxButton" value="ajaxCall" />
<div id="ajaxResult"></div>
</asp:Content>
From the snippet you provided there are a couple of things to consider:
You are missing a script reference to the Microsoft ASP.NET 2.0 AJAX Templates for Visual Studio 2008.
You are using the jquery's document.ready function (raised whenever the DOM is ready to be traversed and manipulated) instead of the System.Application.init event (raised after all scripts have been loaded but before objects are created).
Can you try this to see if it works for you:
<script src="../../Scripts/jquery-1.2.6.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjaxTemplates.debug.js" type="text/javascript"></script>
<script type="text/javascript">
var myView;
Sys.Application.add_init(pageLoad);
function pageLoad() {
myView = $create(Sys.UI.DataView, {}, {}, {}, $get("ajaxResult"));
$("#callAjaxButton").click(callActionMethod);
}
function callActionMethod() {
$.getJSON("/Home/GetCategories", bindData);
}
function bindData(data) {
myView.set_data(data);
}
</script>
<input type="button" id="callAjaxButton" value="ajaxCall" />
<div id="ajaxResult"></div>
Scott Hanselman has written a nice post on this subject.