I'm trying to access a Google Protocol RPC API with jQuery like this:
jQuery.ajax({
url: some_url,
type: "POST",
data : some_params,
contentType: "application/json; charset=utf-8",
dataType : "json",
success : function (data) {
console.log(data);
},
error : function (xhr, status, errorThrown) {
console.log(xhr);
}
});
But I only get the following error message:
XMLHttpRequest cannot load https://*****. Response for preflight has invalid HTTP status code 400
How can I return an OPTIONSmethod in ProtoRPC, that I don't get this error anymore?
Related
I already created MVC spring and I want consume with SAPUI5 (javascript) with AJAX but I found an error "415 (Unsupported Media Type)". I use swagger in spring for test CRUD. in swagger, I success for insert data but failed in AJAX.
controller Spring:
#PostMapping(value={"/tesinsert"}, consumes={"application/json"})
#ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<?> insert(#RequestBody KasusEntity user) throws Exception {
Map result = new HashMap();
userService.insertTabel(user);
return new ResponseEntity<>(result, HttpStatus.CREATED);
}
in javascript:
var data = {
"kodekasus":5,
"nama":"baru",
"isdelete":1,
"createdby":"hahaa",
"createddate":null,
"updatedby":"hihii",
"updateddate":null
};
$.ajax({
type: 'POST',
url: url,
data: data,
success: function(data) {
console.log('sukses: '+data);
},
error: function(error){
console.log('gagal: '+error);
}
});
if I code above in AJAX, show error "415 (Unsupported Media Type)", if I add in AJAX show different error: "Response for preflight has invalid HTTP status code 403
":
headers: {
Accept : "application/json; charset=utf-8",
"Content-Type": "application/json; charset=utf-8"
}
How to solve this problem?
Thanks.
Bobby
Add dataType: 'json' in your ajax call:
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'json',
success: function(data) {
console.log('sukses: '+data);
},
error: function(error){
console.log('gagal: '+error);
}
});
I can successfully reach XRMServices on ORGANIZATION_URL/XRMServices/2011/OrganizationData.svc/AccountSet?$select=AccountNumber and retrieve customer account number on a browser after logging in. However, there is an authentication service blocking this if I use AJAX. My code is as below
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: ORGANIZATION_URL+ "/XRMServices/2011/OrganizationData.svc/AccountSet?$select=AccountNumber,Telephone1,Telephone2,new_CustomerDiscGroup,EMailAddress1,EMailAddress2,EMailAddress3",
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
console.log(XMLHttpRequest);
},
complete: function (XmlHttpRequest) {
console.log(XMLHttpRequest);
},
success: function (data, textStatus, XmlHttpRequest) {
console.log(data);
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
console.log(textStatus);
}
});
What am I missing???
Most likely you are doing cross-site scripting error. So you are opening your CRM using for example http://localhost or http://ip_number, and then in your ajax call you are using ORGANIZATION_URL which is probably different (for example http://contosocrm). Make sure that you are calling your ajax request with exactly the same address as you use to access CRM (or the page that is calling ajax)
After a huge pain, here is the answer to logging in to Dynamics CRM using jQuery
$.ajax({
url : 'https://<Your Authentication URL for CRM>/adfs/ls',
data : {
UserName : '<username>',
Password : '<password>',
wa : 'wsignin1.0',
wauth : 'urn:federation:authentication:windows',
wtrealm : '<CRM Location>',
wct : 'YYYY-MM-DDTHH:MM:SSZ'
},
headers : {
Accept: 'image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap,application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*','Content-Type': 'application/x-www-form-urlencoded','Access-Control-Allow-Origin' : '*'
},
crossDomain: true,
dataType: 'jsonp',
beforeSend : function(xhr){
console.log(xhr);
},
complete : function(xhr){
console.log(xhr);
},
success : function(xhr){
console.log(xhr);
},
error : function(xhr){
console.log(xhr);
}
});
Hope that helps someone
I'm trying to use the postmates API, which first requires us to authenticate ourselves using http basic authentication. The username field in the code below is where we inserted our private API key.
<script>
$(document).ready(function(){
// Request with custom header
$.ajax({
url: ' http://username:#api.postmates.com',
type: 'GET',
dataType: 'jsonp',
success: function(response) { alert("Success"); },
error: function(error) {alert(error); }
});
});
</script>
The error we are getting is
XMLHttpRequest cannot load
http://api.postmates.com/?callback=jQuery112008309037607698633_1462052396724&_=1462052396725.
Response for preflight is invalid (redirect)
need the authentication
https://postmates.com/developer/docs#authentication
The actual header that is used will be a base64-encoded string like
this:
Basic Y2YyZjJkNmQtYTMxNC00NGE4LWI2MDAtNTA1M2MwYWYzMTY1Og==
Try to
$(document).ready(function(){
// Request with custom header
$.ajax({
url: ' http://username:#api.postmates.com',
type: 'GET',
dataType: 'jsonp',
success: function(response) { alert("Success"); },
error: function(error) {alert(error); },
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic Y2YyZjJkNmQtYTMxNC00NGE4LWI2MDAtNTA1M2MwYWYzMTY1Og==");
}
});
});
I don't test because jsfiddle block external petitions.
With Chrome REST service I do this: a post to a certain url, I send a json string in the body, I set the content type as application/json and when I execute the post I get the proper answer.
I am trying to do the same post with jquery.
I first try with:
var beacon = {"beaconid.minor":2,"beaconid.UUID":"beaconnr","beaconid.major":1};
$.ajax({
type: "post",
data: JSON.stringify(beacon),
contentType: "application/json",
dataType: "jsonp",
crossDomain: true,
url: "myurl"}).done(function() {
alert("success");
}).fail(function()
{
alert("error");
});
by I seem to get no answer, I don't get the success alert nor the error alert.
I have then tried with:
var jqxhr = $.post( "myurl", {"beaconid.minor":2,"beaconid.UUID":"beaconnr","beaconid.major":1}).done(function(data, textStatus, jqXHR)
{
}).fail(function(jqXHR, textStatus, errorThrown)
{
alert(textStatus);
});
At least now I get an alert with the textStatus as an error. It is something...but not enough. I could I do a successful post?
The issue is you're setting dataType: "jsonp" for a POST request. JSONP doesn't support POST requests, only GET requests.
I suggest changing to dataType: "json" to see if the service supports CORS. If it doesn't then you'll have to do something like proxy the request through the local server, to get around CORS.
You should not be sending "post" as "type", rather as "method"
And like pointed above, the data type should be json and not jsonp.
$.ajax({
method: "post",
data: JSON.stringify(beacon),
contentType: "application/json",
dataType: "json",
url: "myurl"}).done(function(response) {
alert("success");
//if in chrome
//console.log(response);
}).fail(function(error)
{
//if using Chrome
//console.log(error);
alert(error);
});
When i run url of following code in Firefox poster it runs successfully, but when i try to run with help of $.ajax in my js file of mobile app it gives me "ERROR". why?
(order_id is not the issue)...
$('#page_custinfo').bind('submit',function(){
$.ajax({
url: "http://localhost/putDetails.php?order_id=3&fname=seth&lname=rahul&email=ol#gmail.com&s_add1=karveroad&products=nokia&order_status=pending&order_total=1000&city=pune&postal_code=033&country=india",
type: 'GET',
contentType: 'application/x-www-form-urlencoded' ,
success : function (serverResponse) { alert("success"); },
error : function (jqXHR, textStatus, errorThrown) {alert("ERROR"); }
});
});