简体   繁体   English

如何使用ajax和php for phonegap与mysql数据库连接?

[英]How to connect with mysql database using ajax and php for phonegap?

I am working with a simple phonegap application. 我正在使用一个简单的phonegap应用程序。 i need to display contents from mysql. 我需要显示mysql的内容。 i know that i cannot use php pages for phonegap. 我知道我不能使用php页面进行phonegap。 instead i use webservices in html to connect with mysql. 相反,我在html中使用webservices与mysql连接。 My ajax webservice using responses is not working. 我的使用响应的ajax Web服务无法正常工作。

$.ajax({
    type:"GET",
    url:"config.php",
});

i have the database connection in my config.php file. 我的config.php文件中有数据库连接。 But the connection to config.php is not at all established. 但是与config.php的连接根本没有建立。 Do i have to include any plugins for ajax or any jscript files. 我是否必须包括ajax的任何插件或任何jscript文件。 Please do help me. 请帮帮我。

lets say getdata.php has method to get data from db which includes config.php 可以说getdata.php具有从db中获取数据的方法,其中包括config.php

$.ajax({
type:"GET",
url:"getdata.php",
dataType: "json",
data: { querystring1: querystringvalue},
success: function (data) {
if(data!=""){
parse json and do whatever you want on success
}else{

show some error when there was no data
}
});

Hope it helps 希望能帮助到你

假设您要检索汽车列表,您需要做的就是将config.php包含在get_cars.php中-然后,当您执行以下操作时,此文件将返回数据库中所有汽车的JSON向它发出AJAX请求。

Assuming getdata.php is returning a JSONP object. 假设getdata.php返回一个JSONP对象。 This also assumes you are using JQuery. 这也假设您正在使用JQuery。

$.getJSON("url/to/service?callback=?", function(result){
   //interact with result here
});

By denoting a callback you will avoid the issue of the browser blocking cross origin access. 通过表示回调,可以避免浏览器阻止跨源访问的问题。

This will allow for a separation of concerns. 这将使关注点分离。 Your getdata.php can interact with the config.php server side for all database connections while the phonegap page will only have to use getdata.php to populate the page dynamically. 您的getdata.php可以与config.php服务器端进行所有数据库连接的交互,而phonegap页面只需使用getdata.php即可动态填充页面。

First check if the function where $.ajax({ type:"GET", url:"config.php", }); 首先检查该函数是否在$ .ajax({type:“ GET”,url:“ config.php”,});中; is kept is getting called or not, if it is then try echoing sumthing in config.php check ur console, the data being pass and the response coming, is it the same u want if so then the connection is establish. 是否保持调用状态,如果尝试在config.php中回显求和检查控制台,正在传递的数据和即将到来的响应,是否是您想要的,然后建立连接。

After the connection to config.php is establish then go on and get what is done, now getting the data to the ajax function, you have to echo from config.php in json format which is more appropriate. 建立与config.php的连接后,继续进行操作,现在将数据传递给ajax函数,您必须以json格式从config.php回显,这是更合适的。 You can use 您可以使用

.done(function(msg){ } .done(function(msg){}

where msg is the content u echoed from config.php 其中味精是您从config.php中回显的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM