Ajax request with script tag inside - javascript

On a website i have 2 ajax function, the 1st one is working but the second is sending me script tag with the JSON.
On local everything is working well and one of my AJAX function is working so i presume that my problem dont come from my host.
Here is screenshot of my code & the return.
screenshot of ajax func
screenshot of what i get

The site you are making an ajax request to is returning the data + the script.
If that site is yours then you should make it return the data only and never include scripts in APIs.
As you can clearly see in the received data that it returns the script + the data.

So i fixed it, im using symfony, the problem came from my controller.
In my return i was sending an object which isnt "jsonify" so it gaves me script tag to do it.
But i still dont know why it worked on local

Related

Using Datatables with jQuery and ajax for gathering data gives a strange URL

So i'm working on a project where I have been able to fetch data out from the database to a JSON endpoint on my server, looking simular to this: http://some.com/api/herearejson. I then want to load the data into a table and keep refreshing this table every once in a while.
I'm using Django as by backend and I have come across a javascript library called Datatables (https://datatables.net) and have been setting it up properly. To sum up what I have done is to create a table in the HTML with the top-row-naming of the table. I then use Datatables-framework in javascript to get the data from the JSON file using an ajax-call provided by the framework, to the endpoint of my app.
This is based on the example as shown in the documentation (https://datatables.net/examples/ajax/simple.html):
$(document).ready(function() {
$('#example').DataTable( {
"ajax": "data/arrays.txt"
} );
} );
This part works like a charm, however after digging into what is really going on I discovered that whenever I do an Ajax-call the following GET-command is sent
XHR finished loading: GET "https://some.com/api/herearejson?_=1492196827012". jquery-1.12.4.js:10254
The same message can be shown in the console when loading the example above.
I was wondering if someone can tell me what the last query-like statement at the end of the URL is?
?_=1492196827012
To me it looks like some kind of counter that only increases but I don't know where this comes from and why it's there?
It's bothering me not been able to find out what it is, and I'm sceptical of using it since I don't really get where it comes from. Can this have any affect on my application in the long run?
I hope someone can help me figure this out.
This is anti-cache parameter so browser will always load data from server, not cache.

Using JavaScript to send String to Servlet, and Results from servlet back to JavaScript

First of all: sorry for my bad grammer. English isn't my native language, but i will try to exlpain my problem as simple as i can.
I'm working on a web-application, where user can enter a link. (Question 1) This link should be send to the server/servlet and will be progressed to other things. (Question 2) After the progression, the servlet will send a json-array (?) back to the javascript-part of my app.
I'm completly new to this kind of stuff, but its very important to me, to find out how this works or better, how i can make this work. Its actually very simple, but i used plenty of weeks and cant figure it out.
The application is using the SAP UI5-libs (Question 3), where i would also like to know, if there is any possible way, to parse JSON with the UI5 libs.
I hope, i could explain my problem good enough, so i can get some help. Thanks to all!
The 'sending' of the string to the server/servlet would happen via ajax in either POST or GET form. That is up to you.
I recommend you use a javascript plugin like jQuery (JQuery Ajax API) because the regular ajax code is a bit messy.
As for the servlet/server communicating back to the client is as simple as writing to the page. In a typical servlet context it would be something like
out.print("This is a message");
where Ajax automatically returns the content of the entire page upon callback.
So in conclusion:
Consider test.jsp your servlet. I wish to send "Hi" from the client (being the browser) via GET to the servlet and I want the servlet to say "Hello" back.
I would open an ajax request of type GET to the url "test.jsp?param=Hi". In the servlet I receive this page request and process it. The servlet discards the parameter because it is not used and outputs "Hello" to the page.
In the client the ajax will have returned "Hello" and I can use this to put it into a var or whatever and all of this happened while not refreshing and not navigating in the original document where I did the javascript.
Another way is using websockets where you basically use sockets in javascript to send and receive any kind of data.
Also please check out this possible duplicate question: How to send a string to a servlet from javascript using xmlhttprequest

ajax post big data Jquery

I am facing problem for sending big form data using ajax function to server i.e php page. In my page html input fields will add by users for making a quote. And user add many fields then all datas wont posted to server php page. Only some information will save in database and others will lost.
I have used post method in ajax function.
If you have any suggestion for this problem please suggest me.
Detect place of the problem
Check POST request. Did you see there your data. In case you see - there is only one reason some where on the server. It couldn't be limit of the post - because in this case you will never get any information on the server and all request would be cancelled.
Another place is in your javascript:
1. you make mistake in name of field
2. bug some where in library
3. bug in your code.
try to debug javascript and understand what happen there.

Know how of Cross Domain Request in Jquery

Can anyone please explain how jquery handles cross domain requests? I understand the theory that it does via script using src attribute as url. But i was trying to test the same thing in plain javascript . I need to know the sequence of activities to be done for a post request. at what stage the data is sent and script element is constructed ? I am tired of asking the same at different forums where i got to see links explaining CORS. i need a to-do solution here.
Thanks
PS: sorry if i am asking too much :)
The ajax request URL is set as the .src attribute on a dynamically generated script tag and a parameter is added to the URL &callback=someFunc where someFunc is a local javascript function. When the server receives the URL, it's job is to parse the generate javascript that contains the returned data and then calls the passed in function name with the data as an argument. This is usually referred to as JSONP.

can't make window.location.reload() inside the ajax call

Problem
I am not able to refresh the page with window.location.reload() which is used inside the success call made to yahoo.
Any hints how it can be fixed.
The whole of the code is working fine it is making call to cse server getting contents from there saving on yahoo. but i have to manually refresh the page to bring the contents. I want it to be automatic so I used window.location.reload() but thats not working. Any suggestions how it can be done. The function below is actually a function for a button.
That's the problem, right there.
If your script is running from the CSE server's domain, you cannot send data to the yahoo server. This is javascript's main limitations. Likewise, if running off of the yahoo domain, you can send data to it, but cannot send data to the CSE server, unless it is part of the yahoo domain.
Would work:
Get data from blahblahblah.yahoo.com, then send data to somedomain.yahoo.com
Would not work:
Get data from blahblahblah.somesite.com and send data to somedomain.yahoo.com
Main point, if you're getting data from "csce.unl.edu" and running off of that domain (aka running your script in a browser window from that domain), you can only send data to a site that ends with ".unl.edu". So you can send or receive from "test.unl.edu", but not some yahoo site.
A solution:
Host a proxy script on some webserver, or write all of your code in PHP. Here is two great references on what a proxy script is, and the second link actually provides one for you:
Link 1
Link 2
Any more help needed, you can let me know, I had to set one up myself, on my server, and I can help you out if you run into problems.
did you tried:
window.location = window.location;

Categories