I'm having issues with submitting data to Rails app from Opera browser. Chrome, Firefox, Safari and IE all seems to be working, but Opera is failing at PATCH request.
My script is:
var sendForm = function(type){
if (type == "POST")
var url = window.location.protocol + '//' + window.location.host + '/account/surveys/' + $('#survey-id').val() + '/respond';
else
var url = window.location.protocol + '//' + window.location.host + '/account/surveys/' + $('#survey-id').val() + '/respond_upd';
$.ajax({
type : type,
url : url,
dataType: 'json',
contentType: 'application/json',
data : JSON.stringify({
"survey": {
"id": $('#survey-id').val(),
"survey_answers_attributes": responses(),
"user_qualities_attributes": serializeQualities()
}
})
});
};
So POST and PATCH are using pretty much the same thing, just targeted at different urls.
Now POST works fine, I get it all working and Rails log shows following params:
parameters: {"survey"=>{"id"=>"66", "survey_answers_attributes"=>[{"question_id"=>"1", "response"=>"Don't know", "survey_id"=>"66", "user_id"=>"155"}, {"question_id"=>"2", "response"=>"Don't know", "survey_id"=>"66", "user_id"=>"155"}, {"question_i....
PATCH however results in params like that:
parameters: {"id"=>"66", "survey"=>{}}
Any idea what might be happening here? This issue is killing me
Related
I have an strange problem with Ajax.
var base_url = 'https//m4th.nl/'; // Alle groepen van deze school ophalen
$.ajax({
type: "POST",
async: false,
url: base_url + "rapportage/get/groups/",
success: function(data) {
$.each($.parseJSON(data), function(i, item) {
$('select[name="leerling-groep"]').append('<option value="' + item.id + '">' + item.naam + '</option>');
});
}
});
But in my console i get this error:
"NetworkError: 404 Not Found - https://m4th.nl/rapportage/https//m4th.nl/rapportage/get/groups/"
Very strange because input of the url is ok but the code duplicate the url.
Does somebody know what i did wrong?
Thanks!
change this line
var base_url = 'https//m4th.nl/';
to
var base_url = 'https://m4th.nl/';
you are missing the colon after https making the browser to think of the base url as extension -url
I'm have a problem loading a site. Using this code:
$.ajax({
type: "POST",
//url: '#Url.Action("/TellFriendPopup")',
url: '/Property/TellFriendPopup',
data: { "SenderName": SenderName, "senderMail": senderMail, "receiverMail": receiverMail, "comments": comments, "urlLink": urlLink, "subjectId": subjectId },
success: function (data) {
$("#result").html("<ul><li>Name: " + data.nameret + "</li><li>Email: " + data.emailret + "</li><li>Message: " + data.messageret + "</li></ul>");
$(".dialog").dialog("close");
},
The problem is that I had to move the code to a JavaScript file, instead of the MVC4 View, where i could use the #Url.Action method. But it is not working in JavaScript. It Just gives me this error POST http://localhost:54717/Property/ContactPopup 404 (Not Found). The reason as I can see is that it's the Globalization that it's missing. Because the url should look like this http://localhost:54717/da/Property/ContactPopup or http://localhost:54717/en/Property/ContactPopup
You could get the first folder of the pathname. As long as that is where the language code is on every page.
var language = location.pathname.split("/")[1];
url: language + '/Property/TellFriendPopup'
You can have language in hidden field.
var language = document.getElementById('language`).value;
url: '/' + language + '/Property/TellFriendPopup'
Can you try changing this: url: '/Property/TellFriendPopup',
to this url: '../Property/TellFriendPopup',
I could use another pair of eyes. This script does what I want in Chrome (renders a gallery of images from a google picasa web album), but doesn't render anything in IE8, can anyone see anything obvious?
$(function () {
var json_Photo_URI = "https://picasaweb.google.com/data/feed/base/user/111727095210955830593/albumid/5420114965919213729?alt=json&fields=entry(title, media:group)&thumbsize=75"
$.ajax({
type: 'GET',
url: json_Photo_URI,
success: function (data) {
var photo_URL = "";
var photo_Thumb_Url = "";
$.each(data.feed.entry, function (i, item) {
$.each(item.media$group.media$content, function (i, item) { photo_URL = item.url; });
$.each(item.media$group.media$thumbnail, function (i, item) { photo_Thumb_URL = item.url; });
var photo_Description = item.media$group.media$description.$t;
$('#gallery').append("<a href='" + photo_URL + "' title='" + photo_Description + "'><img src='" + photo_Thumb_URL + "'></a>");
});
$('#gallery a').lightBox({ txtImage: 'Photo', txtOf: '/' });
}, dataType: 'json'
});
});
Replace:
dataType : 'json'
with
dataType : 'jsonp'
See: http://jsfiddle.net/b36v5/2/
It's about cross domain AJAX requests. The site you are trying to access correctly sets the access-control-allow-origin: * response header in order to allow AJAX requests for browsers that support CORS. And IE supports CORS, partially. In fact in IE in order to support CORS you have to use a different object called XDomainRequest as explained in the following blog post in order to achieve cross domain requests. Except that jQuery.ajax doesn't support it out of the box. And if you look more carefully in the bug ticket you will see that it is closed because there are plugins that allow to achieve that.
I am debugging my solution on localhost and this piece of code is not hitting the server
var cartInfo = $j("#ctl00_BaseContentPlaceHolder_ctl00_updateShoppingCart").html();
var dataString = 'name=' + name.val() + '&email=' + email.val() + '&phone=' + phone.val() + '&date=' + date.val() + '&cart=' + cartInfo;
$j.ajax({
type: "POST",
url: "LightBoxContactSender.aspx",
data: dataString,
success: OnSuccess,
error: OnError
});
What am I missing here?
the are 2 potential problems
the url
the data
aspx assumes webforms, the page life cycle, etc. when all you want is to send response and get a reply. instead try targeting either a
web service (asmx)
generic handler (ashx)
a static pagemethod think ajax server hanlders embedded within a page object
second is the data. instead of passing a string as the data try passing a json object
data = {
name: "john",
email: "john#email.com",
...
}
Since you are passing the cartInfo as an html, it should be url encoded before it is posted. I think this is wat causing this issue. Try this
var cartInfo =
$j("#ctl00_BaseContentPlaceHolder_ctl00_updateShoppingCart").html();
var dataString = 'name=' + name.val()
+ '&email=' + email.val()
+ '&phone=' + phone.val()
+ '&date=' + date.val()
+ '&cart=' + encodeURIComponent(cartInfo);
$j.ajax({
type: "POST",
url: "LightBoxContactSender.aspx",
data: dataString,
success: OnSuccess,
error: OnError
});
As suggested by in other answer you can also pass data as a key/value pair object to ajax method, jQuery will take care the rest but you still have to encode the html content.
Url Doesnt call any method ?
url: "LightBoxContactSender.aspx",
If yoy are trying to call a WebMethod inside aspx
url :"LightBoxContactSender.aspx/callStaticMethod",
Try to form Data as
dataString = "{'name=' + '"+name.val()+"'}"
I'm trying to submit a form to a website not my own (salesforce.com with their web-to-lead function) and I'm doing it through a javascript modal window.
Basically once all the entries are tested and made sure there are no errors, I use this:
if(error_count == 0) {
$.ajax({
type: "POST",
url: "[salesforce url]",
data: "first_name=" + first_name + "&last_name=" + last_name + "&email=" + email + "&firm=" + firm + "&[salesforceid]=" + AUM + "&[salesforceid]=" + custodian + "&[salesforceid]=" + custodian_other,
error: function() {
$('.error').hide();
$('#sendError').slideDown('slow');
},
success: function () {
$('.error').hide();
$('.success').slideDown('slow');
$('form#callToAction').fadeOut('slow');
}
});
}
If tested the form without using javascript and the url works, so I'm thinking maybe the way javascript handles the url is the issue?
The issue: the data is not getting successfully submitted to Salesforce. Again, regular HTML form works, javascript doesn't. So I've identified it as a javascript issue.
You cannot make a XHR cross domain request unless the receiving server has allowed it and the browser supports CORS. You can however do a blind submit like this which will assume success:
var $form = $("<form>", {
method: "POST",
action: "[salesforce url]",
target: "my-iframe"
}).appendTo("body");
var $iframe = $("<iframe>", {
name: "my-iframe"
}).bind( "load", function () {
$('.error').hide();
$('.success').slideDown('slow');
$('form#callToAction').fadeOut('slow');
$iframe.remove();
$form.remove();
}).appendTo("body");
$.each(("first_name=" + first_name + "&last_name=" + last_name + "&email=" + email + "&firm=" + firm + "&[salesforceid]=" + AUM + "&[salesforceid]=" + custodian + "&[salesforceid]=" + custodian_other).split("&")), function (index, value) {
var pair = value.split("=");
$form.append("<input>", {
type: "hidden",
name: pair[0],
value: pair[1]
});
});
$form.submit();
+1 for Jim Jose. This sort of thing could be interpreted as an XSS attack against the user, so most likely the browser will not allow it.
You could check out your browser's error logs, see if there's any security errors when you try to run your script. If that doesn't do anything, try to install a tool like Firebug and see if it can pinpoint the problem.
You could also improve your error handling by trying a different sort of method signature for the error function, like this:
error: function(jqXHR, textStatus, errorThrown) {...}
This way you can check what sort of error is being thrown from the Ajax call.