jquery .ajax() unexpected ; - ajax

trying to do some low-level ajax that hits url in the background but does nothing on the client side. Here is the code:
can_id = $(this).attr('id');
$.ajax({
url:"savethis.php",
data:"q=0&can_id="+can_id;
});
but I keep getting this error:
Uncaught SyntaxError: Unexpected token ;
When I comment out the ajax() portion the error goes away. Why am I getting this error?

You have a semicolon at the end of can_id. Remove it
data:"q=0&can_id="+can_id**;**

Related

React js AJAX sends sometimes GET instead of POST and getting 304 strange

I've got a problem and I have no idea why it appears. The circumstances of its appearance are very strange for me...
I've got a POST REST service /login. It expects json {"email":email,"password":password}. I am using ajax and everything works correctly... except for the case when email (is in real format) contains '#' sign and some letters before and after( I know it is strange but only in this case such error appears). When I pass email i.e "mum#mum.com" then few things are happening:
I see that browser sends GET request instead of POST and obtains 304 http status
In the browser console I see infomation "The development server has disconnected. Refresh the page if necessary" and page refreshes automatically
The above things happen only when email is in format I described above.When I pass "aaa" or "aaa#" as email everything works correctly(browser sends POST request and I don't get error in console).
I honestly have no idea why this happens... would be extremely grateful for your help and I will answer all your questions concerning this.
PS.
When I use REST web service tool in IntellJ everything always works fine.
handleLogin() {
const input = {
email: this.state.email,
password: this.state.password
};
$.ajax({
url: CONST.USER_SERVICE + "/login",
type: "POST",
data: JSON.stringify(input),
contentType: "jsonp"
})
.fail(function () {
alert("Wrong data");
})
.always(function (arg1, arg2, arg3) {
if (arg3.status === 200) {
alert("ok!");
}
}.bind(this));
}
Try making the ajax request like data: input without stringify. Ajax expects an object.

Reading cross domain csv file using JSONP and AJAX

Trying to read cross domain csv file :
remote_url = “http://www.example.com/buglist.cgi?bug_status=NEW&columnlist=bug_id%2Cshort_desc&query_format=advanced&ctype=csv";
$.ajax({
url:remote_url,
type:"get",
cache: false,
dataType: "jsonp",
success:function(data){
console.log(data);
},
error: function(data){
console.log(data);
}
});
Although getting 200 status from server, It always going into error callback handler and logging a JavaScript syntax error:
SyntaxError: missing ; before statement 6230,"this is a ""short description"", blah blah blah..
My CSV file has two columns “bug_id” and “short_desc” with following values:
bug_id = 6230
short_desc = this is a "short description", blah blah blah..
I know the error is because of double quote in description, but I don’t know the solution. I tried “YQL” to convert CSV to JSON but it returned null as a result, may be because of an error.
The JSONP technique expects the response to be JavaScript. This is why you're getting a JavaScript error; it's trying to execute the CSV content as JavaScript.
JSONP works by defining a callback function in the server, adding a script tag with a URL that tells the server what function it will call, then the server responds with JavaScript that calls that function with the data as a parameter.
The destination URL must support this protocol in other words. You can't just use JSONP for any arbitrary request.
You may need to use a different technique to get your data, like a same-domain proxy that does the HTTP request to the server. If you are able to change the code at example.com, you have even more options.

Firefox: Failure"nsresult: "0x80004005 (NS_ERROR_FAILURE)

I faced with following error in Firefox 23(this code works fine in IE and Chrome) during send of XMLHttpRequest:
[Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: <unknown filename> :: loadUiDesXml :: line 1" data: no] { message="Failure", result=2147500037, name="NS_ERROR_FAILURE", more...}
I can't google what 2147500037 error means and this error is very strange for me.
This is a piece of code:
var xmlHttp = new XMLHttpRequest();
if (typeof theFile === "string") {
xmlHttp.open("POST", theFile, false);
} else {
xmlHttp.open("POST", theFile.baseURI, false);
}
xmlHttp.send("");
This code is executed from "onload" handler for body element.
The file variable is "/emWeb_6-0/des/en/ld117/ept.xml", domen origin policy shouldn't prevent this request.
The most strange thing, that I can recieve ept.xml in other pages.
For example, I can recieve this file successfully in the following scenario:
Recieve ept.xml file on page1.
Redirect to page2, recieve ept.xml again
Redirect to page3, recieve some other files, including ept.xml
Redirect back to page1, recieve status.xml, then I try to recieve ept.xml and I get error duting send("")
The network trace is the following:
POST ept.xml
POST ept.xml
POST pch.xml
POST node.xml
POST ftpRslt.xml
POST ept.xml
POST status.xml
POST status.xml
I can't execute xmlHttp.send("") here.
This code works inside frame, I think that it can be related with the error.
Can anyone explain what is the root cause of this?
UPDATE: Found, that there is error in HTTP response for this file:
XML syntax analysis error : address: moz-nullprincipal:{48eaaeb4-b5f1-4557-931a-88cfd0c372c5} Line 1, char2:
But I can resend thhis request and it works after that.

Using ajax to GET a token from external site returns empty response

I have two sites right now. One that has a token and one that is supposed to allow a user to do stuff with the token.
When I visit the first site that has the token, mySite.local/services/session/token it shows it: OTV4Gu9VQfjIo2ioQ0thajdEJ6nEINoxsLuwgT_6S0w
When I am on the page that is supposed to GET this token, I get an empty response and the error for the ajax function is thrown.
The weird part is that when investigating the issue with firebug, I can see the response for the ajax request is 43B - the same size as the token. So for some reason the page with the token is being hit properly, but the response is not coming through.
Here is a screenshot of the firebug response:
And here is the JQuery with the ajax request:
var nid; //global node id variable
$('html').click(function(){
try {
$.ajax({
url:"http://mySite.local/services/session/token",
type:"get",
dataType:"text",
error:function (jqXHR, textStatus, errorThrown) {
alert('error thrown - ' + errorThrown);
console.log(JSON.stringify(jqXHR));
console.log(JSON.stringify(textStatus));
console.log(JSON.stringify(errorThrown));
},
success: function (token) {
//Do some stuff now that token is received
}
});
}
catch (error) {
alert("page_dashboard - " + error);
}
});
Your running into the Same Origin Policy which essentially states any request done by client side/browser language like Javascript must be on the same port, with the same domain name and the same protocol. In your case http://mysitemobile.local does not equal http://mysite.local so you're request is being blocked. Firebug's way of displaying that is no response with 43 bytes.
There are two ways to work around this, Cross-origin resource sharing (CORS) or JSONP. CORS is a HTTP header that is added to the server you are requesting to and provides a whitelist of acceptable domains that are allowed break the same origin policy. Most recent browsers support this header.
The other option is JSONP, wraps a JSON object into a Javascript function that is called using <script> tags normally. If the other server returns {status: 0} and you have a function called parseStatus() in your code that the remote server would wrap into parseStatus({status:0}); thus calling your function without having to worry about the same origin policy.

JSP+Struts: Handle session timeout in AJAX calls

I am making ajax calls from my JS methods to invoke the action class's methods in the following manner:
$.getJSON("treeDemo_!getRootNode?appId=" + applicationId, function () {
}).success(function (e) {
}).error(function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}).complete(function () {
});
Session configuration in my web.xml looks like this
<session-config>
<session-timeout>1</session-timeout>
</session-config>
Now when the session expires after 1 minute, the ajax call returns error (without calling the action method). The error says
textStatus = parsererror
errorThrown = SyntaxError: JSON.parse: unexpected character
But this looks like a generic error. I want to catch the session timeout exception and redirect the user to the login.jsp page in case this exception occurs.
I even tried handling it by doing this. But it didn't work for me.
Can you please suggest how should I solve it?
There's no " session timeout exception ".
Your Ajax call is expecting to get JSON back, nothing in your app sends JSON back if there's a session timeout. Use a filter or interceptor as in the link, check for session timeout, handle normally if it's a normal request, but if it's an Ajax request send back an HTTP error code.
Handle this error code in your JavaScript callbacks.

Resources