how do you pass a parameter to an asp.net page method using jquery/ajax? - asp.net

I'm trying to pass parameters to my static web method (which is in an asp.net page). I'm trying to pass the "test1" param with a value of "myvalue". Any ideas on what I am doing wrong?
$.ajax({
type: "POST",
url: "WebForm1.aspx/WebMethod1",
data: {"test1": "myvalue"},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
}
});

my "data" section was wrong. it needs to be "{'test1':'myvalue'}"

What error are you getting?
I've used prototype before (similar to jQuery for ajax) and with that you don't quote the parameter names. So your data parameter should probably be:
data: {test1: "myvalue"}
Give that a shot.
You could also try setting up Fiddler and see the actual request being made.

The way you have it set up, any error that may occur in the ajax call will get silently swallowed. I'd suggest adding an error callback like this:
$.ajax({
type: "POST",
url: "WebForm1.aspx/WebMethod1",
data: {"test1": "myvalue"},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
},
error: function(response) {
$('body',document).html(response.responseText);
}
});
Also, if you are using Visual Studio, you could run your ASP.NET app in debug mode to catch any server-side error. You can also put a breakpoint somewhere in the server-side code to make sure it's getting hit at all, and to inspect the Request.Form collection.
Hope this helps.

Related

Upload file size limit with custom webscript on Alfresco Community 5.2

I need help to upload document content back in Alfresco Community 5.2 though a share javascript.
The destination noderef is already existing, I upload a new version of a document.
I can't use the api/upload web service because I also need to do some operation on the noderef and I have a base64 content of the file which need to be converted.
So I wrote a new webscript and it is all working fine, at least while I upload documents which are smaller than 3MB,
Here it is the code
Alfresco.util.Ajax.request({
method: Alfresco.util.Ajax.POST,
dataObj: {
bytes: response.bytes,
digestAlgorithm: response.digestAlgorithm,
mimeType: response.mimeType.mimeTypeString,
name: response.name,
nodeRef: this.nodeRef,
signatureLevel: this.signatureLevel
},
url: thisClass.urlAlfrescoService + "myOrg/myPackage/uploadDocument",
successCallback: {
fn: thisClass._successOnUploadContent,
scope: this
},
failureCallback: {
fn: thisClass._errorOnUploadContent,
scope: this
},
scope: this,
noReloadOnAuthFailure: true
});
Do I miss some option to increase max upload file size?
I tryed uploading the file normally (with drag and drop) and it works.
The problem is when the file is >= 3MB the java class behind the webscript does not receive any byte
UPDATE
After some researches I found it could be a problem of how data are passed through POST, as application/x-www-form-urlencoded instead of multipart/form-data, but I can't find a way to specify the request content type in the ajax request
SOLUTION
The problem was the application/x-www-form-urlencoded instead of the multipart/form-data, I used a fetch POST request as stated here, but also the ajax request solution is good.
Last week,I had a same very similar problem with Alfresco AJAX request on Alfresco 5.0.2.5 and I used jquery's AJAX calls and it worked for me.
$.ajax({
url: Alfresco.constants.PROXY_URI + "your_web_script",
type: "POST",
data: dataFromFiles,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
dataType: "text",
success: function(data, textStatus, jqXHR) {
},
error: function(jqXHR, textStatus, errorThrown) {
}
});
Reference link : https://blog.arvixe.com/sending-multipart-form-using-ajax/
Hope this helps you.

Instagrams oEmbed endpoint and a simple URL append endpoind API is returning max width of 658px

I am trying to get a max width of 822px for the Instragram images but end up with getting the max-width of 658px. I am using oEmbed endpoint and a simple URL append endpoint. The code snippet is below. I checked the Instagram Developer Documentation, they said that "MAXWIDTH" value is optional and must be greater than 320. https://www.instagram.com/developer/embedding/#oembed
jQuery.ajax({
url: "https://api.instagram.com/oembed?url=" + publicUrl,
data: {omitscript:true, maxwidth:822, hidecaption:data.info.imageCaption},
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
cache: false,
success : function (response) {
console.log(response);
},
error : function () {
console.log("error in ajax");
}
});
Any idea, why is this happening ?

How to run code if yepnope returns true?

I use yepnope.js to load a polyfill (Intl). If window.Intl doesn't exist, it loads my polyfill, and executes some code. However, if windows.Intl does in fact exist, I need to execute another code, and I havn't been able to figure out how.
Here is what I have:
yepnope({
test : window.Intl,
nope : ['lib/Intl.min.js'],
callback: function(u,r,k){
$.ajax({
type: "GET",
url: 'sv-SE.json',
dataType: "json",
success: function(data) {
Intl.__addLocaleData(data,"sv-SE");
self.numberFormatter = new Intl.NumberFormat("sv-SE");
}
});
},
complete: function(){
/* Do stuff! */
The code I need to run if window.Intl already exists is self.numberFormatter = new Intl.NumberFormat("sv-SE");, but I can't figure out how, as callback is only called on false, as far as I can tell.

AJAX call not working when async: true

So I want this AJAX-call to run asynchronously but it only works with async: false
var url = 'Default.aspx/CheckLogin';
$.ajax({
async: true, // works with async: false
type: "POST",
contentType: "application/json; charset=utf-8",
url: url,
data: '{"email":"' + email.val().trim() + '", "password":"' + password.val().trim() + '"}',
dataType: "json",
success: function (data) {
alert("success!");
},
error: function (result,something,error) {
alert("Error: " + error);
}
});
EDIT:
When async is true the alert will show immediately with "Error:", that is no error msg.
The CheckLogin function is pretty slow so when it actually runs it it takes a while.
Any idea of what I'm doing wrong?
EDIT 2:
This is some info I got from chrome://net-internals/#events:
t=1381258516496 [st= 0] +REQUEST_ALIVE [dt=12]
t=1381258516497 [st= 1] +URL_REQUEST_START_JOB [dt=11]
--> load_flags = 142737665 (ENABLE_LOAD_TIMING | MAYBE_USER_GESTURE | REPORT_RAW_HEADERS | VALIDATE_CACHE | VERIFY_EV_CERT)
--> method = "POST"
--> priority = 2
--> upload_id = "0"
--> url = "http://localhost:1176/web_new/Default.aspx/CheckLogin"
t=1381258516497 [st= 1] HTTP_CACHE_GET_BACKEND [dt=0]
t=1381258516497 [st= 1] +HTTP_STREAM_REQUEST [dt=11]
t=1381258516508 [st=12] CANCELLED
t=1381258516508 [st=12] -HTTP_STREAM_REQUEST
t=1381258516508 [st=12] -URL_REQUEST_START_JOB
--> net_error = -3 (ERR_ABORTED)
t=1381258516508 [st=12] -REQUEST_ALIVE
Async true is the default for ajax. There are times when you need to set it to false, for example, you might make another ajax call inside your callback , in that case, you will need to set outer ajax call to async false and the one in the callback to true.
Also, since your making a call to a code behind, make sure to format your data correctly and the codebehind webmethod is static. Other than that, your ajax looks ok.

How do you send a notification from server using UrbanAirship?

I am trying to send a notification from the server and the docs says to POST to /api/push/broadcast/ which I have done from the following code
$.ajax({
type: 'POST',
dataType: 'json',
url: 'https://go.urbanairship.com/api/push/?callback=?',
data: '{"android": {"alert": "hi"}}',
contentType: "application/json",
username:"P4...UBg",
password:"fg...gDA",
error: function(jqXHR, textStatus, errorThrown){
// log the error to the console
alert(
"The following error occured: "+
textStatus, errorThrown
);
},
});
And I am getting a 500 (Internal Server Error). I added the callback to prevent "same origin policy" error as suggested here. Does anybody know how to do it correctly?
Thanks
I initially tried to set things up this way (making a raw $POST) to their servers. In the end, I was happy to find that they have modules for most common languages. Our server is all written in Python so we used the Python module. Here is a list of the packages they have:
https://support.urbanairship.com/customer/portal/articles/60713-push-server-libraries
All we had to do was upload the .py file into our bin and we're able to send pushes like this:
airship_android = urbanairship.Airship(SECRET_KEY, MASTER_KEY)
push_data = {"android": {"alert": notification_message, "extra": str(extra_data_str)}, "apids": [apid]}
airship_android.push(push_data)
Hopefully that helps!

Resources