WebService in JqueryMobile(Response in Json) - javascript

I'm doing a project in Jquery Mobiles and need an webservice. Normally in android we have the http connection and we got the response as xml/json etc.. We parse the data and processed to the view. Here,In case of jquery , facing problem to solve the issue.. I read that ajax call is using for the webservices.
Sample code
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "some url",
data: "{}",
dataType: "json",
success: function (msg) {
alert(msg);
//$("#resultLog").html(msg);
}
});
Also,I refer the google api_Place Search, the service for this is entirely different
RefernceLink Click HERE!
Does anyone suggest the proper way to implement the webservice to interact with my server and retrieve the json response?
Thanks in Advance!

Related

Problem while using AJAX requesting webmethod info from VB file

I've been trying to call a VB.NET function from JavaScript for the website I've been working on, and found out about AJAX and started trying to use it.
(For reference, my target framework is 4.6.1.)
The following is a snippet of my VB.NET code, where PopulateDetailSection() is something which returns a string which is supposed to be the text from the div, but my breakpoint never hits this function.
System.Web.Services.WebMethod(EnableSession:=True, BufferResponse:=False)
Protected Shared Function PopulateDetail() As HtmlGenericControl
Return PopulateDetailSection()
End Function
and as for the AJAX call:
jQuery.ajax({
url: "frmActiveTrackingG.aspx/PopulateDetail",
type: "GET",
//contentType: "application/json: charset=utf-8",
dataType:"html",
success: function (data) {
alert(data);
}
});
I've tried alerting several things, but it keeps returning undefined unless I alert data which appears to return the aspx file starting with the header.
I usually don't ask questions here, but I'm truly stumped on this.
There some issues with your JavaScript. As described here: https://stackoverflow.com/a/5332290/428010 you need to post on the web page/method.
jQuery.ajax({
url: "frmActiveTrackingG.aspx/PopulateDetail",
type: "POST",
contentType: "application/json: charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
}
});
On the VB side the method need to be public not protected.

IIS URL Rewrite and ajax calls in Asp.NET Core 2.2

I have a Asp.NET Core 2.2 site at localhost:81 and I have set up a IIS URL Rewrite to make it accessible from
www.mydomain.com/mysite
Note: I have other sites located on other ports on localhost which are published the same way.
The URL Rewriter takes care of rewriting the references in .cshtml files, but it doesn't handle my ajax calls:
$.ajax({
type: "GET",
url: "/Customers/Index?handler=HairColor&customerID=#Model.CustomerID&date=" + myDate,
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: null,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {},
failure: function(response) {}
});
I don't know how the rewrite module could ever know that it should rewrite that specific url in my javascript.
Question:
What is the right way to make the ajax call compatible with my website setup?
I am starting to wonder if hosting multiple sites like this on a single domain
is the wrong approach?

Javascript POST to API

So I am a bit lost and hoping you can help me out. I am writing an app in simple PHP/HTML/Javascript app.
My Goal: To POST json data to an API.
How can I go about this? I just can't find any good examples to show me the best way to handle this.
In my request I need to send Basic Authorization as well as the json values.
This is what I have right now
$.ajax({
type: "POST",
url: "host.com/api/comments",
dataType: 'json',
async: false,
headers: {
"Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxx"
},
data: '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}',
success: function (){
alert('Comment Submitted');
}
});
I can't get the above code to work. Im using a button to call a function that will start the ajax call but nothing is happening.
Any help be be amazing! Thank You.
Use
contentType:"application/json"
You need to use JSON.stringify method to convert it to JSON format when you send it,
And the model binding will bind the json data to your class object.
The below code will work fine (tested)
$(function () {
var customer = {contact_name :"Scott",company_name:"HP"};
$.ajax({
type: "POST",
data :JSON.stringify(customer),
url: "api/Customer",
contentType: "application/json"
});
});
If you're writing the API in PHP, and it uses $_POST to get the parameters, you shouldn't send JSON. PHP only knows how to decode multipart/form-data and application/x-www-form-urlencode. If you pass an object to $.ajax, jQuery will use the urlencode format.
Just take the quotes off the object that you're passing to the data: option.
$.ajax({
type: "POST",
url: "host.com/api/comments",
dataType: 'json',
async: false,
headers: {
"Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxx"
},
data: {"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}},
success: function (){
alert('Comment Submitted');
}
});
You also shouldn't use async: false, it is deprecated. Learn to write proper async code.
Nobody seems to have addressed one issue - the URL
If the page this is requested from is http://yourhost.com/path/file.html the request will be sent as http://yourhost.com/path/host.com/api/comments
As you have host.com in the URL, I assumed the request is to a different domain?
use one of
http://host.com/api/comments
https://host.com/api/comments
//host.com/api/comments
will only work if your page is loaded http and not https
will work only if the remote API supports https
will only always work properly if the remote API supports both http and https
The other issue is regarding the format of the sent data
The default content-type for $.ajax POST is application/x-www-form-urlencoded; charset=UTF-8
So, sending a POST request with various combinations of contentType and data shows the following
Firstly, without setting contentType
data: '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}'
request is sent as formData '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}'
data: {"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}},
request is sent as formdata, the following values:
value1: 2.0
value2: setPowerState
value3[state]: 0
looks better, because there's actually multiple values, not just a string
Now, let's set contentType
contentType: 'json', data: {"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}},
firefox does not tell me the format of the following string: 'value1=2.0&value2=setPowerState&value3%5Bstate%5D=0' - looks useless
And finally
contentType: 'json', data: '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}',
sends the following JSON: '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}'
So, finally, if the API requires JSON request data, and it's actually on a domain called "host.com"
$.ajax({
type: "POST",
url: "//host.com/api/comments",
dataType: 'json',
contentType: 'json', data: '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}',
});

Use Server.MapPath in jQuery AJAX call while deployed on local IIS

I am using WebMethod on my ASPXpage and I call it from jQuery on the same page like this:
$.ajax({
type: "POST",
url: "WebForm1.aspx/Saveuser",
contentType: "application/json; charset=utf-8",
data: parameters,
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
This works fine in my debug environment, but when I deploy on the hosting site it does not. The problem seems to be in the URL because on the server the path would be different. So I used Server.MapPath in various ways but none of them worked.
Url: '<%= Server.MapPath("~/MyPage.aspx/GetSomeData")%>'
Can anyone overcome this defect?

Application for phonegap with remote database

I made an application based on phone gap 2.9.1 android application with local database
with transaction and create query. but now i want to make remote database for this application so that i can use it from anywhere with same database. Can anyone help me? please..
Thanks..
Make a webservice. connect through it using ajax and parse json
Edited
I tried something like this (this is not web service though)
below is made from ASP.Net
[AllowCrossSiteJson]
public JsonResult AddPerson(Person person)
{
//Get Data here
return Json(*put something here*);
}
and called it in my javascript
function saveData(person) {
var json = $.toJSON(person); //converts person object to json
$.ajax({
url: "http://somedomain.com/Ajax/AddPerson",
type: 'POST',
dataType: 'json',
data: json,
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert("ok");
}
});
}
Edited Part 2
You can do the same in PHP

Categories