HTTPS issue with ajax call in javascript - javascript

I am calling a webservice using ajax call(javascript) to return json.It is working fine.But when i changed my site to HTTPS it is not working.What will be the reason?
function GetReportguid1, callback) {
$.ajax({
type: "POST",
url: "/demo/Datapage.aspx/GetData",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({
guiddemo: guid1
}),
success: function (results) {
callback(results);
},
error: AjaxFailed
});
};

Add the Access-Control-Allow-Origin header from the server
Access-Control-Allow-Origin: https://www.yoursite.com
http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing
And try use JSONP instead.

Related

Calling web service from code behind instead of javascript

I have a javascript code that calls a web service from another server, it's working, but i need to call it from code behind due to an http call request issue, following my code :
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://10.8.111.12:7181/CatchEventLink.asmx/CollectDataLink",
data: JSON.stringify({ "pDataDictionnary": lDataCollected }),
dataType: "json",
async: true,
success: function (data, textStatus) {
if (textStatus == "success") {
//alert('success');
}
},
error: function (exception) {
//alert('Exeption : ' + exception);
}
});
Any suggestions please??

ajax request not working in google chrome - net::ERR_INCOMPLETE_CHUNKED_ENCODING

I have a plain html page and I write some javascript code that use jquery ajax request. The ajax request sends to server successfully in firefox and IE, but does not work in google chrome! When the ajax request sent, chrome console shows net::ERR_INCOMPLETE_CHUNKED_ENCODING error. I don't know How can I solve this strange problem. Here is my ajax code:
var data = {};
data['type'] = "email";
data['value'] = "test#gmail.com"
$.ajax({
headers: {
Accept: "application/json; charset=utf-8",
"Content-Type": "application/json; charset=utf-8"
},
type: "POST",
url: "/testUrl",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(result) {
alert("success");
},
error: function(data) {
alert("error");
},
failure: function(errMsg) {
alert("failure");
}
});
I tested plain ajax request with XMLHttpRequest javascript object, but also has above problem.

ERROR: bad URI when sending data as JSON with GET method to a Rails resource

Hello I'm performing a GET request on a RESTful Rails resource, like so:
function getGroups(category) {
$.ajax({
type: 'GET',
url: 'http://localhost:3000/groups.json',
data: JSON.stringify({"access_token":"569669d8df0456", "category":category }),
success: function(data) {alert(data)},
contentType: "application/json",
dataType: 'json'
});
});
getGroups("own_groups");
The problem is that Webrick server errors out like this:
ERROR bad URI
`/groups.json?{%22access_token%22:%22569669d8df0456%22,%22category%22:%22own_groups%22}'.
It must be something related with how the JSON data is parsed, because I am having no problems with another GET request WITHOUT JSON data, and a POST request WITH JSON data...
Update: adding code for POST request (where JSON.stringify is required)
function addGroup(name, description) {
$.ajax({
type: 'POST',
url: 'http://localhost:3000/groups.json',
data: JSON.stringify({"access_token":"569669d8df0456", "group_name":name, "group_description":description}),
success: function(data) { alert("ok")},
contentType: "application/json",
dataType: 'json'
});
};
addGroup("nice group", "full of people");
Do not use JSON.stringify. Simply put:
data: {"access_token":"569669d8df0456", "category":category },
Moreover, you do not need to specify complete url http://localhost:3000/groups.json, it can be just simply groups.json

Getting grunt's connect server to accept POST requests on static files

I'm trying to use the connect middleware framework grunt comes preconfigured with to develop the front-end of my application, with static JSON files standing in for actual web services which I'll develop later.
However, sending a POST request to my static file results in a 404 error, even though a GET request with the same URL and parameters works just fine.
Can I configure grunt/connect to simply serve up my static file when a POST request is made to that URL?
I did a trick in my source code to call GET method if the app uses Grunt Server:
var useGruntServer = window.location.href.indexOf("localhost:9000") >= 0;
self.jsonGet = function (url, dataIn, callback, errorCallBack) {
$.ajax({
data: dataIn,
type: "GET",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (callback) callback(result);
},
error: function () {
if (errorCallBack) errorCallBack();
}
});
};
self.jsonPost = function (url, dataIn, callback, errorCallBack) {
//Grunt Server Accepts only GET requests
if (useGruntServer) {
self.jsonGet(url, null, callback, errorCallBack);
return;
}
$.ajax({
data: self.objectToJsonString(dataIn),
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (callback) callback(result);
},
error: function () {
if (errorCallBack) errorCallBack();
}
});
};

Cross domain Ajax Call

While calling MVC webservice from my html page it was always going to error; please help me
$.ajax({
type: "GET",
url: url,
contentType: "application/json; charset=utf-8",
crossDomain: true,
dataType: "json",
success: function (msg) {
alert(Response.ErrorMessage )
},
error: function (e, status) {
alert(e + " Fail " + status)
}
});
You have to setup CORS (http://www.html5rocks.com/en/tutorials/cors/) or use JSONP. If you decide to use JSONP then you have to send proper callback function name with JSON data.

Categories