Ajax call response as question mark instead of Arabic characters - ajax

I am making a ajax call POST request(sending audio as a blob) to the server. I am expecting Arabic characters as response but I am getting question mark instead.
$.ajax({
"async":true,
"url": url,
"method":"POST",
"contentType": false,
"processData": false,
"data":data_audio,
success:function(response){}
Response is coming as - {result: "?????.", status: "success"}

Related

IE9 decline response of http server

IE9 decline response of below ajax request to local http server.
$.ajax({
type: 'GET',
url: "127.0.0.1",
data:{res:"pending"},
dataType: "text",
crossDomain: false,
cache: false,
success: success,
error: error
});
respone of server :
"HTTP/1.0 200 OK\r\n"
"Access-control-allow-methods: *\r\n"
"Access-control-allow-origin: *\r\n"
"Content-Type: text/html\r\n"
"Content-Length: 0\r\n\r\n"
Is there any reason???? what IE9 expects??
I would recommend to take a look at the jQuery contentType parameter and dataType as well. Note that possible values are xml, json, jsonp, text, script, html. The default is Intelligent Guess by jQuery.
As stated in documentation:
dataType: The type of data that you're expecting back from the server.
contentType: When sending data to the server, use this content-type.
You should specify that you are sending HTML type data (by using html as dataType, because your server returns HTML in the response header (Content-Type: text/html), also if you have script tags in returned result - those will be evaluated when inserted in the DOM.

how to reproduce successful curl call in ajax

In Charles proxy I've captured a successful request made when using curl - which returns some json. I've tried all day to make this into an ajax call but have failed - wonder if someone could take a fresh look at it? I'm sending a token via the header to a service that provides this json
{
"category": [{
"id": 1,
"name": "aaa",
"description": "anything..."
}, {
"id": 2,
"name": "ccc",
"description": "desc 1111..."
}, {
"id": 3,
"name": "ddd",
"description": "desc..."
}]
}
charles dump of the good request made with curl
GET /api/v1/category HTTP/1.1
User-Agent curl/7.30.0
Host myurl.com
Accept */*
Authorization Token token=6ff1fed470ec2ddaf8b3af9584619902
the raw info
URL http://myurl.com/api/v1/category
Status Complete
Response Code 200 OK
Protocol HTTP/1.1
Method GET
Kept Alive No
Content-Type application/json; charset=utf-8
Client Address /127.0.0.1
Remote Address myurl.com/23.23.121.64
my attempt
$.ajax({
beforeSend: function (xhr) { xhr.setRequestHeader ('Authorization', 'Token 6ff1fed470ec2ddaf8b3af9584619902') },
type: "GET",
url: "http://myurl.com/api/v1/category",
dataType: "json"
}).done(function( msg ) {
console.log( msg );
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log( "Request failed: " + textStatus );
console.log( errorThrown );
});
thanks in advance!
You haven’t posted the diagnostics for your attempt, so I’m just going to guess from your code.
You send
Authorization: Token 6ff1fed470ec2ddaf8b3af9584619902
from XHR, but
Authorization: Token token=6ff1fed470ec2ddaf8b3af9584619902
from curl—note the token=.
You have to make sure that http://myurl.com/api/v1/category either supports cross-origin resource sharing or is located at the same origin (domain) as the page that executes that JavaScript code.
If these do not help, post whatever error messages you get.

Malformed JSON response being sent by Play Server

I am using jquery Ajax to send data to the client and get reply from the Client.
I am using Play Framework as backend.
AJAX:
$.ajax({
type: "GET",
url: '/authenticate',
data: {'type':type, 'redirectURL':getRedirectURL},
contentType: "application/json; charset=UTF-8",
dataType: 'json'
}).success(function( msg, txtStatus, jqXHR) {
console.log("asdasd5= "+msg);
console.log("asdasd5= "+msg.authUrl);
console.log("asdasd5= "+jqXHR.authUrl);
window.location = msg;
});
SERVER DATA CREATION:
response.setContentTypeIfNotSet("text/plain; charset=UTF-8");
Logger.info("response content type ="+response.contentType);
renderJSON("{\"authUrl\": " + authUrl +"}");
The server is sending something like "www.mywebsite.com/yoohoo/auth/1231"
But the response received by Ajax is �����������������{"authUrl": www.mywebsite.com/yoohoo/auth/1231}
DUE to these weird character the JSON response received cannot be parsed by jQuery.
Kindly, help on this.
Thanks
I noted that you are sending back (server-side) a MIME type of "text/plain". Perhaps switch to "application/json". Additionally, you could take the return string and strip any junk from the string before parsing the JSON out on the client side. This would help sanitize in case you change something again server-side and accidentally introduce new/different "junk".

Uncaught SyntaxError: Unexpected token : using jsonp callback

I am making a cross browser jsonp call in which my backend to which i am sending some values is made using django and getting some after callback to my front end which is in php . The problem is its giving Uncaught SyntaxError: Unexpected token : error The data is being send from the django and i have checked that. i am using the code below to make jsonp calls
$(document).on('click', '.miloginme', function(event) {
var username = $('#username').val();
var password = $('#password').val();
var token = $('#token').val();
var dataString="uid="+username+"&token="+token;
$.ajax({
type: 'POST',
url: "http://localhost:8000/b/authenticate/",
crossDomain: true,
data: dataString,
async: false,
dataType: 'jsonp',
success: function(data) {
alert(data);
}
});
});
the values in callback that i am getting is in format
{"token": "KAMWMS151UWPR4Q", "authenticate": "1", "userid": "brad", "fname": "rahul", "booster_number": "1"}
tldr; The server is not sending back JSONP.
Value reported in the post is JSON (and it is valid JSON) - it is not JSONP. If it were to be treated as JSONP (i.e. evaluated by a <script>) then it would be a syntax error because it is not a valid JavaScript program.
Try this in a JavaScript console:
{"token": "KAMWMS151UWPR4Q", "authenticate": "1", "userid": "brad", "fname": "rahul", "booster_number": "1"}
Error look familiar? (Different browses can return different error messages: Chrome -> "Unexpected token :", FireFox -> "invalid label", IE9 -> "Expected ';'".)
A valid JSONP result would look similar to:
theCallback({"token": "KAMWMS151UWPR4Q", "authenticate": "1", "userid": "brad", "fname": "rahul", "booster_number": "1"})
(The name of theCallback is taken from the jsonp variable passed to the server and the client - e.g. jQuery - creates this function before the injecting <script> element is created so that the function can be invoked using the above syntax.)
Valid JSONP works because the "JSON" data is then run in an expression context (in which it is treated as a valid JavaScript Object Literal), not a statement context (where it is treated as a label and then "some junk" leading to a syntax error).
Common approaches to return JSONP from Django utilize View Decorators:
Django JSONP Decorator
Returning JSON/JSONP from a Django view with a little decorator help
jsonp_decorator.py

Trigger.io Ajax Requests - with Parse Facebook User authentication

I'm trying to authenticate users from Trigger.io, ideally via Facebook.
I authenticate the user via Facebook (using the Parse Facebook module), and pass their access token, acess expiry date, and facebook Id to my call to Parse.
It is here things go wrong. Whenever I try and post this data via Ajax to the Parse REST API, I get an error in my forge/Trigger console reading:
{ type: 'EXPECTED_FAILURE', content: '{"code":107,"error":"This
endpoint only supports Content-Type: application/json requests, not
application/x-www-form-urlencoded."}', statusCode: '400', message:
'HTTP error code received from server: 400' }
The code I used to try and post this data is...
function auth(facebookId,accessToken,expirationDate) {
forge.logging.log('auth started');
forge.request.ajax({
url: 'https://api.parse.com/1/users',
headers: {
'X-Parse-Application-Id': config.parseAppId,
'X-Parse-REST-API-Key': config.parseRestKey,
'Content-Type': 'application/json'
},
type: 'POST',
dataType: 'json',
data: {
"authData": {
"facebook": {
"id" : facebookId,
"access_token": accessToken,
"expiration_date": expirationDate
}
}
},
success: function (data) {
forge.logging.log('auth finished 1');
forge.logging.log(data);
},
error: function(error){
forge.logging.log('auth finished 2');
forge.logging.log(error);
}
})//success
} //auth
I can't figure out how to send this as a JSON object/ in the correct format. If anyone has any ideas they'd be much appreciated. Thanks. Josh.
Whenever the data option passed to forge.requests.ajax is an object like in your example, what actually gets posted is a query string that represents the object. The contentType option merely allows you to set the Content-Type header, it does not effect how objects are encoded for the request.
However if the data option is just a string, then this string is used as the body of the request. You can generate a JSON string to use as the body using JSON.parse like so:
forge.request.ajax({
...
contentType: 'application/json',
data: JSON.stringify({
"authData": {
"facebook": {
"id" : facebookId,
"access_token": accessToken,
"expiration_date": expirationDate
}
}
})
});

Resources