Jquery Ajax & Microsoft JScript runtime error: Syntax error, unrecognized expression: - asp.net-mvc

I am working on the ode-to-food MVC4 demo app on Pluralsite by Scott Allen and have a troublesome error enabling ajax calls. I am thinking this may have something to do with my more recent jquery version but not sure.
When I uncomment following line to activate ajax call I get the Microsoft JScript runtime error, otherwise it works fine:
$('form[data-ucw-ajax="true"]').submit(ajaxFormSubmit);
The error is:
Unhandled exception at line 4411, column 2 in http://localhost:17471/Scripts/jquery-1.9.0.js
0x800a139e - Microsoft JScript runtime error: Syntax error, unrecognized expression:
<div class="wrapper clearfix">
....
</div>
<hr />
My View has this form:
<form method="get" action="#Url.Action("Index")" data-ucw-ajax="true" data-ucw-target="#facilitiesList">
My JQuery code is:
$(function () {
var ajaxFormSubmit = function () {
var $form = $(this);
var options = {
url: $form.attr('action'),
type: $form.attr('method'),
data: $form.serialize()
};
$.ajax(options).done(function (data) {
var $target = $($form.attr('data-ucw-target'));
var $newHtml = $(data);
$target.replaceWith($newHtml);
$newHtml.effect('highlight');
});
return false;
};
$('form[data-ucw-ajax="true"]').submit(ajaxFormSubmit);
});
My Script Load Bundles look like:
bundles.Add(new ScriptBundle("~/bundles/jquery/ucw").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery-ui-{version}.js",
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*",
"~/Scripts/jquery-migrate-1.0.0.js",
"~/Scripts/My.js"
));

"This" has scope issues. The "this" that you refer to in ajaxFormSubmit is the context of the ajaxFormSubmit , not the click handler of the link. You need to do something like this:
$form = $('form[data-ucw-ajax="true"]');
or give the form an ID and use:
$form = $('#myForm');
Also, I would move Jquery to another bundle to ensure it is loaded before all the other libraries.

I have similar error with another code from that video course - when returned html from partial View wrapped into JQuery to apply animation later...
function dataReceived(data) {
var $newHtml = $(data); -<<<<same Exception here
$(target).replaceWith(data);
$(data).effect("highlight");
}
Exception: Unhandled exception at line 4421, column 2 in jquery-1.9.1.js
0x800a139e - JavaScript runtime error: Syntax error, unrecognized expression:
<div id="restaurantsList">.....

I started with the code from this changeset, then, via NuGet:
updated jQuery to 1.9.1
updated jQuery UI to 1.10.0
added jQuery migrate 1.1.0 (also added to jQuery bundle)
I don't see any problems in Chrome or IE10 (except for JQMigrate warnings). Other than the small difference in jQuery versions, I can't see anything that would be different. Can you try to bump up your jQuery reference? If the problem still persists, send me the error message again with 1.9.1

Related

How do I trigger a click event in a unit test for an Angular directive

I have an Angular app (running from an ASP.NET MVC project.)
In this app, I have a directive with the following link function:
link: function (scope, element, attrs, formCtrl) {
element.bind('click', function (event) {
formCtrl.$setPristine();
});
}
I wrote this test:
var $compile,
$rootScope,
form;
beforeEach(module('points'));
beforeEach(inject(function(_$compile_, _$rootScope_, testHelper){
$compile = _$compile_;
$rootScope = _$rootScope_;
testHelper.expectBaseRoute();
}));
it('Should set form to pristine when clicked', function () {
var scope = $rootScope;
form = $compile('<form name="testForm" unsaved-warning-clear><input name="test" ng-model="value"/></form>')(scope);
scope.value = "abc";
scope.$digest();
scope.testForm.test.$setViewValue('def');
expect(scope.testForm.$pristine).toBe(false);
$(form).trigger('click');
expect(scope.testForm.$pristine).toBe(true);
});
If I run that in the browser using Jasmine, the test passes.
If I run it with chutzpah inside the ide, it fails. It never fires the click event.
Is there something I'm missing?
For my case, it was an incident of stupid user error.
My Jasmine test runner file being run from the browser had jquery, then angular referenced.
My chutzpah.json file had it the inverse order.
I moved the jquery reference up and it started working.

jQuery UI not working in WordPress plugin

Simple jQuery UI functions, such as the following:
(function($) {
$('ui').sortable();
})(jQuery);
Result in an error which would normally indicate jQuery UI was not present.
Uncaught TypeError: Object [object Object] has no method 'sortable'
However, Chrome's developer tools confirms that it has been loaded. What's up?
The answer was simply to stop using jQuery shorthand. This:
(function($) {
$('ui').sortable();
})(jQuery);
becomes:
jQuery(document).ready(function($){
$('ui').sortable();
});

kendo editor ReferenceError: uid is not defined

I'm trying to create a image upload with the kendo editor.
I'm always getting an error:
ReferenceError: uid is not defined
...==E&&(E=1),g?(b&&(A+=-b),w&&(I+=-w),e=new
Date(Date.UTC(F,D,E,A,I,H,N))):(e=new ...
kendo.web.min.js (Zeile 11)
I'm using jQuery 1.8.3 and kendoui.web.2013.1.319.open-source
My code is as follow:
<div id="example" class="k-content">
<textarea id="editor" rows="10" cols="30" name="reply-content"></textarea>
</div>
<script>
$(document).ready(function () {
$("#editor").kendoEditor({
imageBrowser: {
messages: {
dropFilesHere: "Drop files here"
},
transport: {
read: "/images/ImageBrowser/Read",
destroy: "/images/ImageBrowser/Destroy",
create: "/images/ImageBrowser/Create",
thumbnailUrl: "/images/ImageBrowser/Thumbnail",
uploadUrl: "/images/ImageBrowser/Upload",
imageUrl: "/images/ImageBrowser/Image?path={0}"
}
}
});
});
</script>
Has someone experienced the same issue?
You are probably returning a list of string, but the editor is waiting for an json result (name, type, size). You can check the demo with a sniffer to see what kind of results is expected from read/thumbnail, etc. Not very sure if you really must implement an server class for Kendo's client equivalent, but, by default, the result for Read is expected as Json for sure.
New finaly i got it to work. I used some workarounds but see your self.
You can also test the functionality on the site: http://jonas.dnsd.me/forum/topic?id=113
But stil it has some bugs like the url: /imageBrowser/?path=/image.jpg. I remove '/?path=' with a javascript function, but it works just for 3 images.
If you upload a image the window will not refreseh.
I would appreciate some ideas about the issues.
Now it is working like a charm ... see at http://jonas.dnsd.me/forum/topic?id=113
I updated to new source code

Getting error on $.mobile.changePage(): Uncaught TypeError: Cannot call method 'trigger' of undefined

I have a JQM apps and I am incorporating Backbone.
Since my initial javascript code is huge, I am only extracting what I believe is problematic.
I am following the advices and calls steps cited here:
jqm-config.js from http://coenraets.org/blog/2012/03/using-backbone-js-with-jquery-mobile/
http://jquerymobile.com/test/docs/pages/backbone-require.html
I have a major problem, and this is the behaviour, the problem comes from this code:
var r = Backbone.Router.extend
router: ...
"page": "pageDisplay"
...
pageDisplay: function(){
c = new AView(); // Backbone.View ...fetch() data...
$(c.el).page(); // Call to JQM to add its extra stuff; seems done correctly
$.mobile.changePage( "#" + c.id, {changeHash: false}); // line 50
}
When following the links of <a href="#page" >, I come as expected to the
page "#page" properly processed. But once there, if I click a refresh, which is indirectly reprocessed by the same router rule, I end up with the following error:
Uncaught TypeError: Cannot call method 'trigger' of undefined
I downloaded the jquery mobile development code and observed this:
// JQM1.1.2 - Line #3772 Show a specific page in the page container.
$.mobile.changePage = function( toPage, options ) {
if ( isPageTransitioning ) {
pageTransitionQueue.unshift(arguments );
return;
}
var settings = $.extend( {}, $.mobile.changePage.defaults, options);
// Make sure we have a pageContainer to work with.
settings.pageContainer = settings.pageContainer || $.mobile.pageContainer;
// Make sure we have a fromPage.
settings.fromPage = settings.fromPage || $.mobile.activePage;
// Line #3788
var mpc = settings.pageContainer, // Line #3789
pbcEvent = new $.Event("pagebeforechange" ),
triggerData = { toPage: toPage, options: settings };
// Let listeners know we're about to change the current page.
mpc.trigger( pbcEvent, triggerData ); // Line #3794
The Uncaught TypeError is caused by Line #3794, because mpc is undefined.
So, from JQM, In the Chrome inspector, I can see also that settings.fromPage is undefined and settings.pageContainer is undefined. I kind of imagine, that JQM cannot make an assumption on the fromPage, and therefore, cannot proceed on my refresh. All the options I have tried on the $mobile.changePage() have not succeed. I am out of ideas.
UPDATE/ Online site with the minimum to reproduce the problem:
apartindex, access the website with the bug
Any help will be appreciated.
The dextoInit function that calls the router code is called in $(document).ready() which does not guarantee that the jQuery mobile page has actually been set up successfully. But the router code calls $.mobile.changePage which depends on jQuery Mobile being initialized.
Putting it into mobileinit or pageinit should work.
(Unfortunately I can't modify the code and test it easily.)
Although, this fix it for the moment, it does have drawbacks. See below.
$(document).bind("pageinit", function(){
console.log('bindtomobileinit: event pageinit received');
if ( !window.AppNode.router ){
window.AppNode.router = new AppNode.singletons.router();
console.log("mobileRouter.js: Starting b history");
console.log('mobileRouter.js: About to launch Backbone history');
Backbone.history.start();
}
});
Registering to pageinit has a weird effect of being fired twice. I see that 2 nodes have been added to the Dom: the default "loading" jquery mobile div (related to pageinit:1), and my data-role page (pageinit:2). So on a "refresh browser click", my situation leaves me waiting for a first pageinit, creating an unexpected jquery mobile dom element (a default page created to display the waiting JQM circle animation), which trigger the router creation, and allows the Backbone.history call which then deal with my "" home page. The second pageinit do not interfere with the settings since I execute it only once.
I am really disappointed by this setup. I will leave this question for now, since it does sort of work.
I've found the source of the problem to be jquery-mobile version 1.3.0. When I fall back to either JSM 1.2.0 or 1.2.1, the "Uncaught TypeError: Cannot call method 'trigger' of undefined" problem goes away.
BTW, I am not using Backbone.
I had fixed this problem by using method append(), but not html()
$('body').append(view.render().$el);
I was able to resolve this issue by changing the page data property from "data-role" to "data-mobile-page" as what is referenced in line 4042 of jqm 1.3.2
fromPage.data( "mobile-page" )._trigger( "beforehide", null, { nextPage: toPage } );
Setting
$.mobile.autoInitializePage = true;
In your jquery mobile config file, some place like:
$(document).on("mobileinit", function () {...});
May help.

jquery ui autocomplete giving an error of "Uncaught TypeError: Cannot read property 'PAGE_UP' of undefined"

I am trying to use the jquery ui autocomplete, and keep having the following error when you type into the input field that has the autocomplete on it:
Uncaught TypeError: Cannot read property 'PAGE_UP' of undefined
I have included the following files on my page:
jquery-1.7.2.min.js
jquery-ui-1.8.21.custom.min.js
jquery-ui-1.8.21.custom.css
Here is the code using the autocomplete:
$('input#searchFor').autocomplete({
source:function(req,add){
$.getJSON("/index.php/search/autoCompleteHandler?q=?&section="+$('input#searchFor').attr("searchDesc"),req,function(data){
var suggestions = [];
$.each(data,function(i,val){
suggestions.push(val.name);
});
add(suggestions);
});
}
});
I have no idea what could be going wrong. Any help would be appreciated.
The jQueryUI example documentation for a remote data source shows the remote data source should be done like:
$(function() {
$( "#birds" ).autocomplete({
source: "search.php",
minLength: 2,
select: function( event, ui ) {
//the code to execute when an item is clicked on
}
});
});
It looks like source only needs to be a url. You could take a look at the ajax request in Chrome to figure out the $_GET or $_POST variable which is being populated with the search query.
It might not be a bad idea depending on your usage to use the remote data source with caching option.

Resources