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'
);
Related
I've checked the gizillion answers to this question and for some reason I can't get this to work.
I get the error:
401 (Unauthorized)
My route is an api route guarded.
My data:
let ajaxMainTemplate = {
'mainTemplate': mainTemplate,
'templateId': templateId,
'_token': accessToken,
}
My ajax call:
$.ajax({
type: 'POST',
url:`../api/aiMainTemplate/${ajaxData.templateId}`,
data: {
_token: ajaxData._token,
ajaxData
},
success: function(response) {
console.log(response)
}
})
I've tried the above to test based on another response. I put the token outside of the ajaxData object. I get the same error. I've also attempted:
$.ajax({
type: 'POST',
url:`../api/aiMainTemplate/${ajaxData.templateId}`,
headers: { 'X-CSRF-TOKEN': ajaxData._token },
data: ajaxData,
success: function(response) {
console.log(response)
}
})
Same.
I've also confirmed the token is there by adding a console.log Any ideas what I'm doing wrong with this?
My url was behind the api protected route so I had to do an ajax call to login into it via ajax.
The credentials are in a variable ajaxData.
$.ajax({
type: 'POST',
url: 'http://127.0.0.1:8000/api/login',
data: ajaxData,
success: function(response) {
shopifyToken = response.success.token;
getListProducts(shopifyToken);
}
});
With the shopify token generated from this login it worked.
I have an entity User[userid, name , age]
Now from jsp I am hitting ajax like this:
$.ajax({
type: "POST",
url: "/user/saveUser.htm",
data: "userid=" + userid+ "&name=" + name + "&age=" + age,
success: function (response) {
alert("success");
}
And my controller is:
#ResponseBody
#RequestMapping(value = {"saveUser"}, method = {RequestMethod.POST})
public String submitProblem(HttpServletRequest req, User user)
{
//backend codes
}
My question is when I am sending name="ABC" , age="24" and id=32;
everything is fine.
But "The request sent by the client was syntactically incorrect." response comes if I am sending id=null.
Please help me to know the issue.
try to use json data in your ajax request first you can use
$.ajax({
type: "POST",
url: "/user/saveUser.htm",
data: {'userid': userid, 'name': name, 'age' : age},
success: function (response) {
alert("success");
}
and try to use corectly with spring i cant help you ive never use spring also your error is type of id
if you convert your id on string maybe this will work because your age work try to do
userid = userid.toString();
$.ajax({
type: "POST",
url: "/user/saveUser.htm",
data: "userid=" + userid+ "&name=" + name + "&age=" + age,
success: function (response) {
alert("success");
}
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 am using jquery ajax to post username and password and return with a result it with working perfectly with GET method but using the post method it send the data but not return with the html result
here is my code:
$.ajax({
type: "POST",
url: "panel.aspx",
data: username + ";" + pw,
success: function (result) {
$("#midiv").html(result);
}
});
Try;
$.ajax({
type: 'POST',
url: 'panel.aspx',
data: {
'username=' + uname "&password=" + pword,
//Note:- uname and pword are variables and not text
},
success: function (result) {
$("#midiv").html(result);
}
});
in your aspx, you may catch data something like;
Dim uname, pword
uname = Request.Form("username")
pword = Request.Form("password")
Hope this helps...
Try this:
$.ajax({
type: "POST",
url: "panel.aspx",
data: {
username: "foo",
pw: "bar"
},
success: function (result) {
$("#midiv").html(result);
}
});
The way you are doing you are sending the variables in the GET request URL. To send data via post, define an object in the data config which the keys represent the parameters that you want to send.
You are not sending the data correctly, try:
$.ajax({
type: "POST",
url: "panel.aspx",
data: 'username=' + username + "&password=" + pw,
success: function (result) {
$("#midiv").html(result);
}
});
$.ajax({
type: "POST",
url: "panel.aspx",
data: {username: username, password: pw}
}).done(function(result) {
$("#midiv").html(result);
});
You'll also need to change your serverside scripts to listen for POST requests and not GET requests, for instance in PHP it would be:
$user = $_POST['username'];
$pw = $_POST['password'];
Not really sure how to do it in aspx, but I'm guessing you'll figure that out ?
I am facing this issue
405 (HTTP method GET is not supported by this URL)
whereas the method is post in my code
$.ajax({
url: myUrl + "?" + "token=" + AccessToken + "&key=" +dev_key,
jsonp: 'callback',
dataType: 'jsonp',
type: 'post',
data: sendXML,
success: function (result) {
alert("hjgh");
}
});
function callback(json)
{
alert("ghj");
}
You specified 'jsonp' as your 'dataType'. That will cause the method to be 'GET', overriding your 'type' settings.