I am trying to perform this AJAX post but for some reason I am getting a server 500 error.
$.ajax({
url: '.../PublicAPI.svc/Register',
type: 'POST',
dataType: 'json',
async: false,
//contentType: 'application/json; charset=utf-8',
data: '{ "ApiKey": "'+$('#ApiKey').val()+'", "LanguageId": "'+$('#LanguageId').val()+'", "Password": "'+$('#Password').val()+'","Username": "'+$('#Email').val()+'" }',
success: function( response, textStatus, jqXHR ) {
console.log('success');
console.log(response);
},
error: function( jqXHR, textStatus, errorThrown ) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
Can any body help me to find out what is the issue?
You are using three dots in your AJAX URL. Try using 2 dots, and the server error should go away.
url: '../PublicAPI.svc/Register',
See Three or more dots followed by a slash in url causes an internal server error
Related
So my problem is: I Have Lumen server on subdomain api.domain.com
After sending ajax request from domain.com to api.domain.com:
$.ajax({
url: 'https://api.domain.com/login',
type: 'post',
headers: {
'Authorization': 'someJWTTOKEN'
'Content-Type': 'application/json'
},
data: {},
dataType: 'json',
success: function(result) {
console.log('result');
console.log(result);
},
error: function(error) {
console.log(error);
}
});
Now I get CORS error:
But when I remove Header from ajax request, everything works fine.
This works great:
$.ajax({
url: 'https://api.domain.com/login',
type: 'post',
headers: {
},
data: {},
dataType: 'json',
success: function(result) {
console.log('result');
console.log(result);
},
error: function(error) {
console.log(error);
}
});
I had this problem connecting my React app to my Lumen API but solved it by using this PHP Package. In the README they explain how to configure the package with Lumen. Legit took me like 2 minutes.
php code
<?php
$data = trim(api_get('threads',array(),'JSON'));
echo $_GET['jsonp_callback'] . '(' . $data . ');';
?>
js code
<script>
$.ajax({
url: "https://abc.secure.tttt.com/",
data:{},
dataType: 'jsonp',
cache: false,
crossDomain: true,
jsonp : "jsonp_callback",
success: function(params){
console.log('showInbox');
//console.log(JSON.parse(params));
showInbox(params);
},
error: function (xhr, textStatus, thrownError) {
console.log('Inside showInbox ajax failure');
console.log(xhr.status);
console.log(textStatus);
console.log(JSON.stringify(xhr));
console.log(thrownError);
}
});
this is working fine, if the API is giving json data and if there an error, it is showing syntax error:
Timestamp: 5/9/2013 4:28:24 PM
Error: SyntaxError: syntax error
Source File: https://abc.secure.tttt.com/?jsonp_callback=jsonp1368095332316&_=1368097103961
Line: 1
Source Code:
<!DOCTYPE html>
Use timeout property to get error function executed on error.
$.ajax({
timeout : 2000,
url: "https://abc.secure.tttt.com/",
data:{},
dataType: 'jsonp',
cache: false,
crossDomain: true,
jsonp : "jsonp_callback",
success: function(params){
console.log('showInbox');
//console.log(JSON.parse(params));
showInbox(params);
},
error: function (xhr, textStatus, thrownError) {
console.log('Inside showInbox ajax failure');
console.log(xhr.status);
console.log(textStatus);
console.log(JSON.stringify(xhr));
console.log(thrownError);
}
});
In most browsers there is no way of telling if the JSONP request succeeded, other than a timeout. Of course set it to a reasonable value, I use 2 seconds guessing if the server doesn't return the file in this time, something is wrong.
I'm firing an HTTP POST request with Ajax to my php file, but I don't get the desired result. $_POST and $_GET are both empty. I think I'm overlooking something, but I have no clue what.
Here's my code for firing the request:
this.save = function() {
alert(ko.toJSON([this.name, this.description, this.pages]));
$.ajax("x", {
data: ko.toJSON([this.name, this.description, this.pages]),
type: "post", contentType: "application/json",
success: function(result) { alert(result) },
error : function(jqXHR, textStatus, errorThrown) { alert(textStatus + errorThrown)}
});
};
Note that I alert the JSON on line 3. That JSON is correct, so the input on line 5 is valid.
My test method in PHP:
header('Content-type: application/json; charset=utf-8');
echo json_encode(array_merge($_POST, $_GET));
exit;
The response I'm getting is an empty array.
I tested the input (see above);
I know the Ajax call itself succeeds, if I replace that second line in my PHP example with json_encode(array('success' => true)); I get that back in my page - so the URL is correct.
I tested it with both GET and POST, with similar negative results.
You are sending a JSON request, that's why both $_POST and $_GET are empty. Try sending the data like this:
$.ajax("x", {
data: { data: [this.name, this.description, this.pages] },
type: "post",
success: function(result) { alert(result) },
error : function(jqXHR, textStatus, errorThrown) { alert(textStatus + errorThrown)}
});
Now look inside $_POST["data"].
or if you need to use a JSON request then you need to deserialize it back in your PHP file:
$.ajax("x", {
data: { data: ko.toJSON([this.name, this.description, this.pages]) },
type: "post",
success: function(result) { alert(result) },
error : function(jqXHR, textStatus, errorThrown) { alert(textStatus + errorThrown)}
});
and then decode:
$json = $_POST['json'];
$data = json_decode($json);
and if you want to send pure JSON request in the POST body:
$.ajax("x", {
data: ko.toJSON([this.name, this.description, this.pages]),
type: "post",
contentType: 'application/json',
success: function(result) { alert(result) },
error : function(jqXHR, textStatus, errorThrown) { alert(textStatus + errorThrown)}
});
and then:
$data = json_decode(file_get_contents("php://input"));
Notice that php://input is a read-only stream that allows you to read raw data from the request body.
I am not sure why but the post to my external PHP file is not working. The post request is not being received by the PHP file as nothing is being outputted.
Here's my jQUery;
$.post("AJAX/get_track_info.php", { url: "uploads/19c9aa51c821952c81be46ca9b2e9056.mp3"}, function(info){
$('#loadInfo').html(info);
});
And in the PHP file is just a
$trackurl = $_POST['url'];
Try something more like this
$.ajax({
type: 'POST',
url: 'AJAX/get_track_info.php',
data: { url: "uploads/19c9aa51c821952c81be46ca9b2e9056.mp3" },
dataType: 'text',
success: function (data, textStatus, jqXHR) { },
error: function (data, textStatus, errorThrown) { }
});
I am sending request header using ajax jquery as follows
$.ajax({
url: "http://www.example.com/index.php",
dataType: "json",
type: "GET",
beforeSend: function(xhr) {
xhr.setRequestHeader("X_REST_USERNAME", "XXXX");
xhr.setRequestHeader("X_REST_PASSWORD", "XXXX");
console.log(xhr);
},
success: function(data, textStatus, XMLHttpRequest) {
alert(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
}
});
i can see the above request header in firebug ,but at the server end on a php page when i try to access following variables it gives me blank , i tired dumping and printing variable in php but there isnt any entry for it
$_SERVER['HTTP_X_REST_USERNAME']
$_SERVER['HTTP_X_REST_PASSWORD']
$_REQUEST['X_REST_USERNAME'];
$_REQUEST['X_REST_PASSWORD'];
In case of apache, use apache_request_headers.
$headers = apache_request_headers();
echo $headers['X_REST_USERNAME'];
echo $headers['X_REST_PASSWORD'];