Getting the URL from the ajax response using EmberJS - ajax

Can anyone help me in how to get the url, from the Ajax response in Ember.
This is to handle a specific use case in which server redirects to another url and returns data from the new url(302 redirect).
If i am able to get the url form the response, then i can check if the requested url is different from the response url and can take the action accordingly.
Note: If we use Angular (tried with Angular4), then we can get the response details including url and header from the response, if you use Http service from '#angular/http'
I am using following config for EmberJS
-ember-cli: 2.14.2
-node: 8.1.0
-os: darwin x64

In comment you specified that you fetch the record using Ember.$.ajax. Ember.$ is not more than an alias for jQuery. So Ember.$.ajax is the same as jQuery.ajax. Following jQuery.ajax documentation success function is called with jqXHR object as third argument. This is a superset of browsers native XMLHttpRequest object. XMLHttpRequest object provides a read-only property responseURL which is what you are looking for.
In general I would recommend to not use jQuery.ajax in modern ember application. There are two great alternatives:
ember-fetch provides a HTML5 fetch polyfill from github wrapped and bundled for ember-cli.
ember-ajax provides a service for making AJAX requests which provides RSVP promises.
There is also work ongoing to migrate away from jQuery.ajax in ember-data.

Related

Intercept HTTP Request in page

I am trying to make a scraper for a page of a supermarket. I noticed that this supermarket make API HTTP calls via AJAX, and if I enter to Inspect > Network then I can see the request headers of the calls I need.
Inside the requests headers there is an X-Token. If I use this X-Token with the URL via Postman I can get all the info in JSON format, which is better than scraping the web.
The problem is that this X-Token expires (I think, it still works). Is there any possibility to make a call to the page and "intercept" this API call in order to retrieve this X-Token and use it for the next custom requests?
I'am using Ruby on Rails :)

AWS API gateway really works well but I can't run this on javascript AJAX

I made python function using AWS lambda and connected lambda with API Gateway
After then, I tested API. It worked well.
Testing in API Gateway was Successful
Now I tried to using this API with AJAX.
Javascript AJAX Code was like this
How ever result was
"jquery-3.4.1.js:9837 GET https://9i1jhuewmj.execute-api.ap-northeast-2.amazonaws.com/test/transaction?jpgname=image.jpg net::ERR_FAILED"
How can i solve this problem??
Hope for your wisdom!
Thank you
I think there are a few things. The content-type header being returned is application/json but the response is not JSON.
But I think the main problem is that the HTTP status being returned is 301. This tells the browser that this resource has been moved and the browser typically expects the response to contain information on where things are moved to so it can redirect.
I suspect if you change your configuration so that a more normal response code (i.e. 200) is returned, this will work better.

jqgrid calling an api controll post method

I am using jqgrid to retrieve data from api controller post method. I have set the jquery url,datatype and mtype like below.
url: ..api/ControllerName,
datatype: "json",
mtype: 'POST',
Problem 1: But it is not hitting the controller.
Problem 2: I also want to sent some validation data with this url to preform some validation then allow data retrieval to user.
I would recommend you to use Developer Tools of Chrome/Internet Explorer/Firefox (see Network part) or the tool like Fiddler available for free to trace HTTP traffic and to verify whether the POST request will be do sent to the server.
Depend on what API controller you use it can be that the method will be not called by the framework because not all input parameters can be initialized by the request. Typically the framework used by API controller on the lower level will return an error back to jgGrid. You can see the response in the HTTP traffic. I recommend you to use loadError callback of jqGrid to display an error message in such case. See the answer for more details.
To send additional parameters to the server you can use postData parameter of jqGrid. I recommend you to read the answer for more details.
By the way some standard parameters will be sent by jqGrid to the server. You can use prmNames option of jqGrid to rename the standard parameters or to remove some from there (by usage null as the value of the corresponding prmNames property).
I found some help jqGrid With ASP.NET Web API

Best Practice for Handling AJAX requests from website to API provider

So, I implemented an API provider to be accessed by both web application and mobile applications.
Most likely this will not be a large scale project, but I want to maximize my learning experience and geek out where I can.
Anyway, from what I understand, it seems like it's better to put the API provider service and the actual website on separate domains to make scaling easier.
For example, twitter has the website twitter.com and api.twitter.com.
One immediate issue would be dealing with the cross-domain issue with AJAX.
From what I gather, there are 2 ways to implement cross-domain AJAX
JSONP: I heard about it, but don't know much beyond the name
Proxy Server: so, my website is build on top of ASP.NET MVC and I was thinking about creating a APIProxy controller to handle all cross-domain API requests.
That way, I would make an AJAX call via $.ajax(settings) and then pass in the website URL that corresponds to the APIProxy controller. The APIProxy controller would then make the appropriate POST server calls and process the JSON responses and return the response back to AJAX callback functions.
I heard about flXHR about I don't want to use Flash because devices like the iPad or any a lot of mobile browsers don't support Flash.
Anyway, I just wanted to ask what are some of the best practices in managing a website with the API provider on a separate domain or subdomain.
When you request some JSON, it returns an object or array. Script tags are not subject to the same-domain rule. So instead making an AJAX call, you would essentially do this:
<script src="Http://api.example.com?param1=something&etc"></script>
That would load the JSON, and it would execute as JavaScript.
...But a simple object or array "executing" by itself isn't very useful. So when you request the JSON, you also include the name of a callback function. If the provider sees that a callback was provided, instead of just returning JSON, it actually returns JavaScript: the JSON is passed to your function as an argument.
Then, when the script loads, your function (which you already defined) is called, and given the JSON to work with.
That's JSONP.
Bibliography
Newton, Aaron. "Request.JSONP." Clientcide. 7 Dec. 2009. Web. 28 Jan. 2011.

Ajax And REST: Can I send an ajax request to a REST service to recieve response?

I want to use mootools and SqueezBox class to handle a request to a RESTful service. I don't want to use any server-side script. I am using AJAX. I send a request to the following url using GET method.
http://www.idevcenter.com/api/v1/links/links-upcoming.json
but I receive a 404 error. Is it because cross-site scripting? here is my code:
SqueezeBox.initialize({handler:'url',ajaxOptions:{method:'GET'}});
$('a.modal').addEvent('click',function(e){
new Event(e).stop();
SqueezeBox.fromElement($('a.modal'));
});
In Firebug console, sometimes 'aborted' is shown and sometimes '404'.what is wrong with that?
XMLHttpRequest is subject to the Same Origin Policy; if the document your JavaScript is running within is not from the same origin as the service you're trying to call, the call will be disallowed for security reasons.
There is now a proposed standard for cross-origin resource sharing to address this. It may be that the service you're trying to use supports it; if so, using a browser that implements CORS (recent versions of Firefox and Chrome do, as do some others) may work. IE8 supports it but requires that you do extra work.
You cannot use XMLHttpRequest (that is, ordinary "ajax") to call a service on a server that is not in your domain.
You can, however, use the JSONP trick, which takes advantage of the fact that the browser will load Javascript from other domains. However, the service has to know that you're going to do that, and it has to understand the protocol. That particular service seems perfectly willing to give me a JSON response, but it doesn't pay attention when I give it a "callback" parameter. (I've tried both "callback" and "jsonp" and the JSON blob that comes back is the same, without a function call wrapper.)

Resources