I just got Scott Hanselman's chat app with SignalR working with ASP.NET MVC 4. After hours of configuration, trial and error, and getting different versions of Windows to talk to each other on my home network, it's all working except that I'm left with one issue that I'm not sure how to handle.
This line of javascript has to change, depending on if I'm running the app through Visual Studio or the published (IIS) version:
Works when running within VS:
var connection = $.connection('echo');
Works with published version:
var connection = $.connection('ChatWithSignalR/echo');
When I run within VS, the URL is:
http://localhost:9145/
And the published version is:
http://localhost/ChatWithSignalR
If I don't change that line of code, and try to run the app within VS, using the javascript that has ChatWithSignalR in it, I get an error like this:
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:9145/ChatWithSignalR/echo/negotiate?_=1347809290826
What can I do so that I can use the same javascript code and have it work in both scenarios?
var connection = $.connection('??????');
Note, this is in my Global.asax.cs:
RouteTable.Routes.MapConnection<MyConnection>("echo", "echo/{*operation}");
This is something you need to take care of because the SignalR library has no idea where the app is deployed to and what its root address is. Something I always do in web applications is have a global Javascript variable called site_root and set it equal to the absolute URL for the root of the site. Now, to do this, you need server tags to evaluate and print that, something like "<%= RootUrl %>" or whatever the syntax is for your server language. Then, when referencing URLs in Javascript, you should always use site_root + "/echo" (with or without the beginning "/" depending on what's printed by the server variable/method). So you'd have something like:
<script type="text/javascript">
var site_root = "<%= RootUrl %>";
// Later, wherever in your code:
function doSomething() {
var echo_url = site_root + "/echo";
// Now you have an absolute URL for the echo page
}
</script>
Now, I put this in the master layout page that always is included - like a Master Page, or depending on what server language you use. Also, instead of a variable like RootUrl, you might use some method to resolve URLs, and just pass it an empty string or "/" to get the root URL for the application.
Related
I'm trying to familiarize myself with the concept of using script tags. I'm making a ruby on rails app that does something as simple as alert "Hi" when a customer visits a page. I am testing this public app on a local server and I have the shopify_app gem installed. The app has been authenticated and I have access to the store's data. I've viewed the Shopify API documentation on using script tags and I've looked at the Shopify Embedded App example that Shopify has on GitHub. The documentation details the properties of a script tag and gives examples of script tags with their properties defined, but doesn't say anything about where to place the script tag in an application, or how to configure an environment so that the js file in the script tag will go through.
I've discovered that a js file being added with a script tag will only work if the js file is hosted online, so I've uploaded the js file to google drive. I have the code for the script tag in the index action of my HomeController (the default page for the app). This is the code I'm using:
def index
if response = request.env['omniauth.auth']
sess = ShopifyAPI::Session.new(params[:shop], response[:credentials][:token])
session[:shopify] = sess
ShopifyAPI::Base.activate_session(sess)
ShopifyAPI::ScriptTag.create(
:event => "onload",
:src => "https://drive.google.com/..."
)
end
I think the problem may be tied to the request.env. The response is not being read as request.env[omniauth.auth] and I believe that the response coming back as valid may be required for the script tag to go through.
The method that I tried above is from the 2nd answer given in this topic: How to develop rails app for shopify with ScriptTags.
The first answer suggested using this code:
ShopifyAPI::Base.site = token
s = ShopifyAPI::ScriptTag.create(:events => "onload",:src => "your javascript url")
However, it doesn't say where to place both lines of code in a rails application. I tried putting the second line in a js file in my rails application, but it did not work.
I don't know if I'm encountering problems because I'm running the app on a local server or if there is something missing from the configuration of my application.
I'd appreciate it if anyone could point me in the right direction.
Try putting something like this in config/initializers/shopify_app.rb
ShopifyApp.configure do |config|
config.api_key = "xxx-xxxx-xxx-xxx"
config.secret = "xxx-xxxx-xxx-xxx"
config.scope = "read_orders, read_products"
config.embedded_app = true
config.scripttags = [
{event:'onload', src: 'https://yourdomain.herokuapp.com/javascripts/yourjs.js'}
]
end
Yes, you are correct that you'll need the js file you want to include for your script tag publicly available - if you are using localhost for development look into ngrok.
Do yourself the favor of ensuring your callbacks use SSL when interacting with the Shopify API (i.e. configure your app with https://localhost/ as a callback setting in the Shopify app settings). I went through the trouble of configuring thin as the web server locally with a self-signed SSL certificate.
With a proper set up you should be able to debug why the response is failing the omniauth check.
I'm new to the Shopify API(s), but not Rails. Their documentation leaves a lot to be desired.
Good luck to you sir,
I have a problem regarding the javascript window.location.href function.
On the development server(Windows server 2008 with IIS) we have an asp.net application deployed in the "default web site" named Topics2 (it's a test application - the production one is Topics).
My problem is:
When i want to navigate to another page using javascript window.location.href the browser redirects me to the production page and not the test page.
Example: Instead of bringing me to server/Topics2/MyPage.aspx?id=100 it brings me to server/MyPage.aspx?id=100 (the Topics name is not shown).
There is no routing made in the application. Is IIS adding some default routing? Or could there be any routing made (that i'm not aware of) in IIS that is messing this up?
What i tried untill now:
1. window.location.href = window.location.protocol + "//" + window.location.host + "/MyPage.aspx?id=" + item.value;
2. window.location.href= "/MyPage.aspx?id="+item.value;
I know i can always hardcode the "Topics2" name but that's not wanted because we don't want to have that in mind every time we deploy to the production server.
I found the solution, which is pretty simple (shame on me that i didn't think of that!).
So, apparently if I type window.location.href= "/MyPage.aspx?id="+item.value; the server redirects me to the production server but if i type window.location.href= "./MyPage.aspx?id="+item.value; it takes me to the right page.
Try using this:
location.replace(yourUrl);
I have a very strange problem with my rails 4.1.0 application.
Inside local_env.yml I have a variable declared as:
API_URL: 'http://api.myapp.com:3000'
I use this variable in the javascript file app_ready.js.erb like so:
var apiHost = '<%= ENV["API_URL"] %>'
But for some reason when I call the page using this script and inspect the app_ready.js file I can see the line got converted to var apiHost = 'http://api.myapp.com' without the port :3000. Does anybody know what is happening here?
Edit:
Is there some kind of a server sided cache maybe? Because at some point I might have declared API_URL without the port and changed it later. It can't be in the browser cache, because I've actually cleared it and even tried with different browsers with the same results.
Ok, it was the cache. Had to run rake tmp:clear and restart rails server.
I've set up a rails app with faye.
It includes the following code parts:
applications.html.haml:
= javascript_include_tag "http://localhost:9292/faye.js"
discussions.js:
$(function() {
var faye = new Faye.Client('http://localhost:9292/faye');
faye.subscribe("/messages/new", function(data) {
eval(data);
});
});
When I run the app on localhost in dev mode everything works fine.
On the VPS server, it tries to reach my "localhost:9292/faye", when it should be the servers localhost.
Q: Is there a method, or way to get it working with the same code on both the server and localhost? Manually changing to the domain isn't looks the rails way.
you can use:
"#{request.protocol}#{request.host_with_port}/faye"
for more info:
How do I get the current absolute URL in Ruby on Rails?
I want to fetch some information from a website using the phantomjs/casperjs libraries, as I'm looking for the HTML result after all javascripts on a site are run. I worked it out with the following code from this answer:
var page = require('webpage').create();
page.open('http://www.scorespro.com/basketball/', function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var p = page.evaluate(function () {
return document.getElementsByTagName('html')[0].innerHTML
});
console.log(p);
}
phantom.exit();
});
And I also worked it out to get phantomjs/casperjs running on heroku by following these instructions, so when I now say heroku run phantomjs theScriptAbove.js on OS X terminal I get the HTML of the given basketball scores website as I was expecting.
But what I actually want is to get the html text from within a Mac desktop application, this is the reason why I was looking for a way to run the scripts on a web server like heroku. So, my question is:
Is there any way to get the HTML text (that my script prints as a result) remotely within my Objective-C desktop application?
Or asked in another way: how can I run and get the answer of my script remotely by using POST/GET?
p.s.
I can handle with Rails applications, so if there's a way to do this using Rails - I just need the basic idea of what I have to do and how to get the phantomjs script to communicate with Rails. But I think there might be an even simpler solution ...
If I understand you correctly you're talking about interprocess communication - so that Phantom's result (the page HTML) can somehow be retrieved by the app.
per the phantom docs, couple options:
write the HTML to a file and pick up the file in your app
run the webserver module and do a GET to phantom, and have the phantom script respond with the page HTML
see http://phantomjs.org/api/webserver/