passing parameters containing # in URL for calling webservice using AJAX - ajax

I am calling one of my webservice in which im sending some paramters like key,id,subject line,etc
example:
http://asv.msdasmafetrix.net/public/mobile.ashx?method=getparsedtemplate_contactinfo&emailbody='" + emailbody + "'&subjectline='" + subjectline + "'&contactemailid=" + contactemailid + "&id=" + jasondata.id + "&key=" + jasondata.key
but,in parameters like subject and emailbody # variable is present due to which it is breaking my code.and giving me undefined value.i have even tried encodeURI.got no success though.
my code is:
var uri="http://asv.msdasmafetrix.net/public/mobile.ashx?method=getparsedtemplate_contactinfo&emailbody='" + emailbody + "'&subjectline='" + subjectline + "'&contactemailid=" + contactemailid + "&id=" + jasondata.id + "&key=" + jasondata.key";
$.ajax({
type: "GET",
url: encodeURI(uri),
success: function(msg) {
jasondata = eval('(' + msg + ')');
var subjectline = jasondata.subjectline;
alert(subjectline);
}
});
any help ...!!

Try using encodeURIComponent(uri)
Documentation for encodeURIComponent

Related

Error on Edit Post shows 422 (Unprocessable Entity) in Ajax call

I keep getting a 422(Unprocessable Entity) error, how do I fix this?
The below is my code on ajax call, can someone help me on to fix this problem?
$(document).on('click', '.edit-post', function() {
$('.edit-modal').css('display','block');
$('.posts-table').css('display','none');
var id = $(this).attr('data-id');
var title = $(this).attr('data-title');
var slug = $(this).attr('data-slug');
var category = $(this).attr('data-category');
var featured_image = $(this).attr('data-image');
var body = $(this).attr('data-body');
$('#edit-post-form #edit-title').val(title);
$('#edit-post-form #edit-slug').val(slug);
$('#edit-post-form #edit-category').val(category);
//$('#edit-post-form #edit-image').val(featured_image);
$('#edit-post-form #edit-body').val(body);
$('#edit-id').val($(this).data('id'));
});
//Update New Post
$("#update-post").click(function(e) {
e.preventDefault();
id = $('#edit-id').val();
var title = $('#edit-post-form #edit-title').val();
var slug = $('#edit-post-form #edit-slug').val();
var category_id = $('#edit-post-form #edit-category').val();
var featured_image = $('#edit-post-form #edit-image').val();
var body = $('#edit-post-form #edit-body').val();
var form = new FormData();
form.append('title', title);
form.append('slug', slug);
form.append('category_id', category_id);
form.append('featured_image', featured_image);
form.append('body', body);
$.ajax({
type:'PUT',
url: "/updatepost/" + id,
headers: {
'X-CSRF-TOKEN' : $('input[name="_token"]').val()
},
dataType: 'json',
cache: false,
contentType: false,
processData: false,
data: form,
success: function(data) {
toastr.success('Successfully Updated Post!', 'Success Alert', {timeOut: 5000});
$('#'+ id).html("<tr id='" + data.id + "' class='item'><th>" + data.id + "</th><td>" + data.title + "</td><td>" + data.body + "</td><td>" + data.created_at + "</td><td><button class='edit-modal btn btn-info' data-id='" + data.id + "' data-title='" + data.title + "' data-slug='" + data.slug + "' data-category='" + data.category_id + "' data-image='" + data.featured_image + "' data-body='" + data.body + "'><span class='glyphicon glyphicon-edit'></span> Edit</button></td></tr>");
$('.edit-modal').css('display','none');
$('.posts-table').css('display','block');
}
});
});
The data of the post will show in the edit form, but when I try to submit it after editing some fields, 422 Unprocessable entity keeps showing.

how ajax site wide browsing works on phpfox?

i'm trying to use ajax wide browsing on phpfox but i dont understand how it works,
any idea please ?
i found in static/jscript/main.js this code :
$Core.ajax = function(sCall, $oParams)
{
var sParams = '&' + getParam('sGlobalTokenName') + '[ajax]=true&' + getParam('sGlobalTokenName') + '[call]=' + sCall;
if (!sParams.match(/\[security_token\]/i))
{
sParams += '&' + getParam('sGlobalTokenName') + '[security_token]=' + oCore['log.security_token'];
}
if (isset($oParams['params']))
{
if (typeof($oParams['params']) == 'string')
{
sParams += $oParams['params'];
}
else
{
$.each($oParams['params'], function($sKey, $sValue)
{
sParams += '&' + $sKey + '=' + encodeURIComponent($sValue) + '';
});
}
}
$.ajax(
{
type: (isset($oParams['type']) ? $oParams['type'] : 'GET'),
url: getParam('sJsStatic') + "ajax.php",
dataType: 'html',
data: sParams,
success: $oParams['success']
});
};
I'm trying to fix a module of chat while browsing on my site
Any idea plz ?
To make a link for site wide ajax browsing you do it just like usual, phpfox will figure it out for you.
If you want to make an ajax call in phpfox you do:
$.ajaxCall('module.function', 'param1=value1&param2=value2');
for example:
$.ajaxCall('ad.recalculate', 'total=' + iTotal + '&location=' + sLocation + '&block_id=' + sBlockId + '&isCPM=' + $Core.Ad.isCPM);
Calls the function recalculate in the file /module/ad/include/component/ajax/ajax.class.php and passes the params: total, location, block_id and isCPM

jquery selectin a input in a div

I have the following code to call a ajax POST :
$("a[href=Save]").click(function() {
var thisform = $(this).attr("name");
// Get all the information left in the form
var lname = $('div[name="' + thisform + '"] input[name="lname"]').val();
var fname = $('div[name="' + thisform + '"] input[name="fname"]').val();
var mname = $('div[name="' + thisform + '"] input[name="mname"]').val();
var language = $('div[name="' + thisform + '"] input[name="lang"]').val();
var title = $('div[name="' + thisform + '"] input[name="title"]').val();
var ptype = $('div[name="' + thisform + '"] input[name="ProfileType"]').val();
var vip = $('div[name="' + thisform + '"] input[name="VIP"]').val();
var vreason = $('div[name="' + thisform + '"] input[name="Vreason"]').val();
alert (lname);
//Set the Ajax Request
$.post("..\ajax\profileMod.php", {
'lname':lname,
'fname':fname,
'mname':mname,
'language':language,
'title':title,
'ptype':ptype,
'vip':vip,
'vreason':vreason
};
.done(function(data) {
// php code : echo json_encode(array("name"=>"Called!"));
alert(data.name);
});
// Stop original behavior
return false;
});
I don't know where I went wrong but the code is not working. I'm trying to call a php file using the POST, and showing a message to see if the file got called correctly. All my "var" are getting to right value (I've tested all of them one by one).
I've look at so many post to find the error that my eyes hurt! So I'm asking help!!!
Thank you!
looks like you put a ";" where you should have put a ")"
'vip':vip,
'vreason':vreason
}; <-- problem
.done(function(data) {
// php code : echo json_encode(array("name"=>"Called!"));
alert(data.name);
Should be
'vip':vip,
'vreason':vreason
}) <-- fix
.done(function(data) {
// php code : echo json_encode(array("name"=>"Called!"));
alert(data.name);
i think

How to make XMLHttpRequest work over HTTPS on google chrome?

I researched about this a lot, but couldn't find the magic.
Actually I want to populate a list of city pin code no. using JQuery autocomplete UI. It's a https page. It's working in Firefox but not in Google Chrome. Can anyone help me to resolve this issue. Thanks in Advance.
In the following is my code:
function zipAutoCompletet(prefix) {
jQuery("#" + prefix + "_zip").autocomplete({
source: function(request, response) {
jQuery.ajax({
url: "http://ws.geonames.org/postalCodeSearchJSON",
dataType: "jsonp",
data: {
style: "full",
maxRows: 12,
postalcode_startsWith: request.term
},
success: function(data) {
response(
jQuery.map(data.postalCodes, function(item) {
return {
label:
item.placeName +
(item.adminCode1
? ", " + item.adminCode1
: "") +
", " +
item.postalCode +
", " +
item.countryCode,
value: item.postalCode
};
})
);
jQuery(".ui-autocomplete").css("width", "188px");
}
});
},
minLength: 2,
select: function(event, ui) {
var myString = new String(ui.item.label);
var address = myString.split(",");
jQuery("#" + prefix + "_city").val(address[0]);
jQuery("#" + prefix + "_city").addClass("activated");
jQuery("#" + prefix + "_city").trigger("change");
jQuery("#" + prefix + "_city")
.parents(".row")
.removeClass("error-row");
jQuery("#" + prefix + "_city")
.parents(".row")
.addClass("ok-row");
var countryCode = address[3] ? address[3] : address[2];
countryCode = jQuery.trim(countryCode);
var countryName = jQuery(
"#" +
prefix +
'_country option[value="' +
jQuery.trim(countryCode) +
'"]'
).text();
jQuery("#countryContainer .jqTransformSelectWrapper span").html(
countryName
);
jQuery("#countryContainer .jqTransformSelectWrapper").addClass(
"selected-jqtranform"
);
jQuery("#" + prefix + "_country")
.parents(".row")
.addClass("ok-row");
jQuery("#" + prefix + "_country")
.parents(".row")
.removeClass("error-row");
jQuery("#" + prefix + "_country").val(jQuery.trim(countryCode));
var stateCode = address[2] ? address[1] : "";
stateCode = jQuery.trim(stateCode);
if (countryCode == "US") {
var base = base_url;
base = base.replace("https", "http");
jQuery.ajax({
url: base + "/getStateName",
dataType: "jsonp",
data: { stateCode: stateCode },
success: function(data) {
stateName = data;
jQuery("#jc_state").val(stateName);
jQuery("#jc_state").addClass("activated");
jQuery("#jc_state")
.parents(".row")
.removeClass("error-row");
jQuery("#jc_state")
.parents(".row")
.addClass("ok-row");
jQuery("#jc_state").trigger("change");
formValidate();
}
});
} else {
stateName = stateCode;
jQuery("#jc_state").val(stateName);
jQuery("#jc_state").addClass("activated");
jQuery("#jc_state")
.parents(".row")
.removeClass("error-row");
jQuery("#jc_state")
.parents(".row")
.addClass("ok-row");
jQuery("#jc_state").trigger("change");
formValidate();
}
jQuery("#" + prefix + "_zip")
.parents(".row")
.addClass("ok-row");
jQuery("#" + prefix + "_zip")
.parents(".row")
.removeClass("error-row");
},
open: function() {
jQuery(this)
.removeClass("ui-corner-all")
.addClass("ui-corner-top");
},
close: function() {
jQuery(this)
.removeClass("ui-corner-top")
.addClass("ui-corner-all");
},
change: function(event, ui) {
if (ui.item === null) {
jQuery("#" + prefix + "_zip")
.parents(".row")
.removeClass("ok-row");
jQuery("#" + prefix + "_zip")
.parents(".row")
.addClass("error-row");
$("#" + prefix + "_zip").val("");
}
}
});
}
If you are on https page, browser will block requests to non-secure resources (http).
Regularly you should see some notification about that. Looks like other browsers does not block non secure AJAX requests on secured pages by default, but google chrome does.
In your code, you have hardcoded URL:
url: "http://ws.geonames.org/postalCodeSearchJSON",
If that is cross domain request and it supports HTTPS, you can change it like this:
url: "//ws.geonames.org/postalCodeSearchJSON",
As you can see, protocol is not specified there. Browser will take page default protocol (http or https) and use it to request data.

ajax type:"GET" works for firefox and chrome but not for IE

Hey guys this works for Firefox and Chrome in that it passes the data fine and shows the confirmationpage
but when i run it on IE it just refreshes the page and the data is all NULL while passing
var dataString = 'firstname=' + firstname + '&lastname=' + lastname + '&areacode=' + areacode + '&phonenumber=' + phonenumber + '&emailaddress=' + emailaddress + '&confirmemail=' + confirmemail + '&password=' + password + '&streetaddress=' + streetaddress + '&streetaddress2=' + streetaddress2 + '&city=' + city + '&state=' + state + '&zipcode=' + zipcode + '&month=' + month + '&day=' + day + '&year=' + year + '&services=' + services + '&agreement=' + agreement;
//alert(dataString);
// alert(services);
//var d = new Date();
$.ajax({
// cache: false,
type: "GET",
url: "http://www.vectorcreditsolution.com/js/process.php",
data: dataString,
// dataType: ($.browser.msie) ? "text" : "xml",
success: function(data) {
window.location.href ="thankyou.html";
}
});
return false;
});
1) Have you verified what is arriving at your server (maybe populate a session variable) to ensure your $.ajax() is sending what you think it is?
2) Have you tried
var dataString = $("#formId").serializeArray();
(assuming <form id="formId"...)? And then use that for your data:dataString, element
3) I would expect you would want code something on the server when it is done processing to respond back to your calling page, and then in the success:function(retData) evaluate retData to interpret the server's response. If you simply don't care what happens and just want the browser page to march blindly forward to your thankyou.html upon completion of the ajax call, you don't need to include an argument variable in the function:
success: function() {...},

Resources