My jquery ajax call for a json file fails in Internet Explorer.
All other explorers are doing good.
Code :
$.ajax({
url: myurl + "?randomid="+Math.random(),
datatype: "json",
mimeType:"application/json; charset=UTF-8",
success: function(data,status,response) {
var JSONdata = $.parseJSON(response.responseText);
alert("Success");
},
error: function(e1,e2,e3) {
alert(e1 + " " + e2 + " " + e3);
}
});
Internet Explorer throws the alert in the error section :
[Object Object] error No Transport
Thanks in advance for your help!
You have cross domain request.
You need to set this (before ajax call):
$.support.cors = true;
Related
I have a UserControl (.ascx) and I want to do an Ajax control on the email.
this is my ajax call inside the file Login.ascx
$.ajax({
url: "Login.aspx/CheckEmail",
type: 'POST',
data: "{email:'"+email+"'}",
dataType: "json",
success: function (data) {
alert("We returned: " + data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
}
});
and this is my webMethod in file Login.aspx.cs
[WebMethod]
public static string CheckEmail(string email)
{
return "it worked";
}
Now, I always have the Error message that is telling me: Internal Server Error
What is wrong here?
thanks
You can't place the WebMethod inside user controls, you have to place it inside a page or inside a service.
Hope this helps.
My Ajax call is really simple as below:
function ajax(reqUrl, params , callback) {
console.log("Request URL "+reqUrl);
var cond;
cond = $.ajax({
type: 'post',
url: reqUrl,
data: params,
error:function(){ alert("some error occurred") },
success: callback
});
console.log("Server response "+cond.readyState);
}
// Call it as
var url = "/getResult";
var params = {};
params.param1 = "test1";
params.param2 = "test2";
ajax(url, params, function(returnCallback) {
console.log(returnCallback);
alert("Success");
});
That works fine in most cases. But sometimes (about 1 times in 3) it doesn't return anything to callback.
I found many questions and answers for Not working ajax in Safari but fine in chrome and FireFox. My problem is different from them, because it's fine most of the time (I don't mean it was not fine usually because when I refresh my browser, that may cause my ajax call to work).
My main question is why does my ajax call sometimes fail? I don't get any errors on my JS console. When this situation, I refresh my browser to get my ajax call to. Any Ideas?
Update:
I found that sometimes my ajax call method didn't call out because console.log("Request URL "+reqUrl); did not execute. When I don't want to refresh my browser, I clicked many times on my page's link to produce result. will something late to execute?
Finally, I found error .. Safari doesn't reload my JavaScript files again even disable Cache. So I put all of my JS code into:
$(document).ready(function(){
// start load my js functions
init();
});
to reload my JS files when my page was ready. Cheer !
I also met this problem.
When I moved all code into $(function() {}), it worked.
After that, I found I had defined a variable named key, which caused the problem.
Just rename it, all things will be running.
This seems to be a Safari issue. In this post there is a suggestion to add a beforeSend to your ajax-request.
In your case:
cond = $.ajax({
type: 'post',
url: reqUrl,
data: params,
beforeSend: function (event, files, index, xhr, handler, callBack) {
$.ajax({
async: false,
url: 'closeconnection.php' // add path
});
},
error:function(){ alert("some error occurred") },
success: callback
});
Please Test below Code. it is working fine.
$.ajax({
type: "POST",
url:'#Url.Action("getResult","Controller")',
data: "{userName :'" + userName + "',password :'" + password + "' }",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.toString());
});
This is use for MVC application.
$.ajax({
type: "POST",
url:'getResult',
data: "{userName :'" + userName + "',password :'" + password + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("here" + data.toString());
});
For Asp.net Application :
$.ajax({
type: "POST",
url:'getResult',
data: "{userName :'" + userName + "',password :'" + password + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("here" + data.toString());
});
if u have still the issue than please post ur complete code here. i will test and reply soon
I'm trying to do a simple odata query and the call is successful, but the results are always undefined. I've thrown the URL into the description, copy and pasted it, and it works just fine. I've tested dozens of different ways to see what the object is, and the results are undefined. What am I missing??
UPDATE: As mentioned below, part of the problem was referencing data.d.results. When I referenced data.d.results[0], I actually got the error message "Unable to get property '0' of undefined or null reference." I wanted to add that here because I found almost NO help when searching for that error message.
The final answer was a combination of:
data.d for only one result
correct casing for system fields; "resProd.Description" as opposed to "resProd.description."
Back to orig Question:
Below is the code I'm using:
function setOPDefaults() {
// Create lookup
var lookupItem = new Array();
lookupItem = Xrm.Page.getAttribute("productid").getValue();
if (lookupItem != null)
{
var guid = lookupItem[0].id;
}
var crmOrg = window.location.pathname.split('/')[1];
var serverUrl = window.location.protocol + "//" + window.location.host + (crmOrg == 'userdefined' ? '' : '/' + crmOrg);
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
var ODATA_PREP = serverUrl + ODATA_ENDPOINT;
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
// Tried both of the following URLS (obviously not at the same time)
url: ODATA_PREP + "/ProductSet(guid'" + guid + "')",
url: "http://crm/<<orgname>>/XRMServices/2011/OrganizationData.svc/ProductSet(guid'67BA90A3-39D8-E211-8D1E-0050569A6113')",
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
var resProd = data.d.results;
alert(resProd.length); // This is undefined
// Below is where I load the URL into description just for testing.
// When I copy and paste this URL into the browser, it pulls up results with correct data
Xrm.Page.getAttribute("description").setValue(ODATA_PREP + "/ProductSet(guid'" + guid + "')");
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert("Ajax call failed: " + textStatus + " - " + errorThrown + " || " + XmlHttpRequest.responseText);
}
});
}
You're acessing just one record, so try put something like that:
data.d
data.d.results is used for multiple results. Another thing you can do to validate the results is put your url directly in browser.
In such cases, I use Fiddler. You can use it to debug your http/https trafic.
Or. If you have crm 2011 RU 12, you can use built-in Chrome debugger. Press F12. In console tab - right click -> Log XMLHttpRequest
Hey all i am trying to figure out why i can not get any returned HTML from my web service.
This is my javascript that calls out to the WS:
function getTVGuide(whatsBeingSent) {
jQuery.support.cors = true;
$.ajax({
crossDomain: true,
async : true,
type: "POST",
url: "http://192.168.9.7/Service.asmx/getTVGuideData",
data: '',
contentType: "application/json",
dataType: "json",
success: OnSuccessCallTVGuide,
error: OnErrorCallTVGuide
});
}
function OnSuccessCallTVGuide(response) {
console.log(response.d);
}
function OnErrorCallTVGuide(response) {
console.log('ERROR: ' + response.status + " " + response.statusText);
}
And the WS is this:
<WebMethod()> Public Function getTVGuideData() As String
Try
Dim sr As StreamReader = New StreamReader("c:\tvlistings.html")
Dim line As String = ""
line = sr.ReadToEnd()
sr.Close()
Return "done"
Catch Ex As Exception
Return "err" 'Ex.Message
End Try
End Function
That works just fine if i only return something like "DONE".
However, if i try returning line then it no longer works. Giving the error:
ERROR: 500 Internal Server Error
I've tried chanign the return values in AJAX to:
contentType: "application/html",
dataType: "html",
and also
contentType: "application/text",
dataType: "text",
But i still get the same error...
What could i be doing incorrect?
ERROR: 500 Internal Server Error means that there's some internal exception in service. You may be getting HTML contents due to CustomError ON and customError module returning a HTML page configured in IIS. CustomError module will change Content-type to text/HTML.
HTH,
Amit
I am trying to send the data via ajax POST method my code is
$.ajax({
url: myUrl + "?token=" + accessToken + "&key=" +dev_key,
dataType: 'jsonp',
type: 'POST',
data: sendXML,
success: function () {
alert("z");
}
});
But the type: 'POST' is not working I am getting the following error on console:
Status Code:405 HTTP method GET is not supported by this URL
Have you tried using $.post ?
Example:
$.post(
myUrl,
{
token: accessToken,
key: dev_key
},
function(result){
alert(z)
}
)
P.S. Isn't ? missing after myUrl?
i think you forgot the ? in the token key like this
mySql + "?token="
otherwise, try this:
jQuery.post(
myUrl + "?token=" + accessToken + "&key=" +dev_key,
sendXML,
function() {
alert('z');
},
'JSONP'
);