Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 jquery ajax

views
     
TSlearner92
post Apr 23 2014, 12:11 PM, updated 10y ago

Getting Started
**
Junior Member
169 posts

Joined: Oct 2011
$.ajax({
type: "GET",
url: "phpFiles/cslogin.php",
data: { staff_id: csID, password: pwd },
datatype :"json" <-----------------
}).done(function( data ) {
var obj = JSON.parse(data);
if (obj==null) {
alert("wrong ID or password.");
}else{
localStorage.setItem('user', data);
alert("Welcome "+obj.staffName+" .");
window.location.href = "customerMain.html";
}
});


for tis method can i change the DATATYPE TO BOOLEAN ,STRING OR INTEGER?
and can some1 tel...i already state the json for the datatype ..how come i still nid to do casting

sunsuron
post Apr 23 2014, 01:31 PM

Regular
******
Senior Member
1,334 posts

Joined: Nov 2004



Check the documentation at https://api.jquery.com/jQuery.ajax/ . The only accepted candidates for the dataType setting are xml, json, script, or html.

If you're expecting JSON data type from cslogin.php, make sure the HTTP header is correct which is: application/json. Something like this:

CODE

$data = ['foo', 'bar', 'baz', 'status' => 0, 'msg' => 'Login Incorrect'];

header( 'Content-Type: application/json; charset=utf-8' );

exit( json_encode( $data ) );


You can also use the shorthand method $.get() if your operation is just to get some data from the server. Something like this:

CODE

var args = $('form').serialize();

$.get( location.href, args, function(response)
{
       console.log( response );
       console.log( response.msg );
});


Documentation at https://api.jquery.com/jQuery.get/ . Here's some tips when dealing with ajax:
  • Use the Developer Tools in Chrome or Firefox to inspect ajax request and response etc.
  • Instead of alert, use console.log to debug.

https://developers.google.com/chrome-developer-tools/
https://developer.mozilla.org/en/docs/Tools
TSlearner92
post Apr 23 2014, 05:13 PM

Getting Started
**
Junior Member
169 posts

Joined: Oct 2011
wow thx for help ^^benefit me alot
however issit correct if i only use ajax() method to deal v php for my whole project?
sunsuron
post Apr 23 2014, 10:06 PM

Regular
******
Senior Member
1,334 posts

Joined: Nov 2004



Yep. You can use $.ajax() or $.post() or $.get() whichever fit your preferences and problem scope.
TSlearner92
post Apr 25 2014, 02:34 PM

Getting Started
**
Junior Member
169 posts

Joined: Oct 2011
can ajax return 2 different data? for exp html + json
sunsuron
post Apr 25 2014, 04:11 PM

Regular
******
Senior Member
1,334 posts

Joined: Nov 2004



From the documentation at https://api.jquery.com/jQuery.ajax/

QUOTE
multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.


Why don't you try it and see for yourself.
TSlearner92
post Apr 26 2014, 08:47 PM

Getting Started
**
Junior Member
169 posts

Joined: Oct 2011
sry... i really new on web application and only left 1 week to do tis assigment...sry btw really appreciate
hemikoo
post Apr 28 2014, 03:49 PM

New Member
*
Junior Member
5 posts

Joined: Jan 2014
QUOTE(learner92 @ Apr 25 2014, 02:34 PM)
can ajax return 2 different data? for exp  html + json
*
it depend on client/server content negotiation.

 

Change to:
| Lo-Fi Version
0.0133sec    0.37    5 queries    GZIP Disabled
Time is now: 28th March 2024 - 09:19 PM