Sunday, 15 July 2012

jquery/PHP: getting NULL return from curl string -



jquery/PHP: getting NULL return from curl string -

i'm seeing peculiar behavior sending json string php jquery's $.ajax function.

when visit php script browser or calling via command line php, returns valid json (tested jsonlint.com). however, when create ajax phone call html file null back.

basically php i'm doing this:

// case in switch handles command 'getinfo': $url = "http://someapi.com:1234/api/entity/" . $tagid; $ch = curl_init( $url ); curl_setopt( $ch, curlopt_header, 0 ); curl_setopt( $ch, curlopt_userpwd, "user:pass" ); curl_setopt( $ch, curlopt_returntransfer, 1 ); $reply = curl_exec( $ch ); header('content-type: application/json'); echo $reply;

this spits out this:

[ { "class" : "entity", "guid" : "it_equipment_abcdef12345677890", "retired" : false, "deletable" : true, "$aassettamper" : false } ]

i've tried both , without returntransfer set, , , without header() line. on html side, calling ajax:

$.ajax({ url: "gimmedata.php", datatype: "json", cache: false, async: false, data: { 'cmd' : 'getinfo', 'tagid' : $('#tag').val }, success: function( data, textstatus ) { // rfcode location console.log(data); } });

the status returns "success", , info "[]".

weird thing is, if re-create output php script , paste script itself, echo string without going through curl, ajax phone call works fine:

echo ' [ { "class" : "entity" "guid" : "it_equipment_abcdef12345677890", "retired" : false, "deletable" : true, "$aassettamper" : false } ]'; return; $ch = curl_init( $url ); curl_setopt( $ch, curlopt_header, 0 ); curl_setopt( $ch, curlopt_userpwd, "user:pass" ); curl_setopt( $ch, curlopt_returntransfer, 1 ); $reply = curl_exec( $ch ); header('content-type: application/json'); echo $reply;

i have no thought can be. clue? curl_exec doing weird things text?

your info passing javascript wrong:

data: { 'cmd' : 'getinfo', 'tagid' : $('#tag').val },

you should utilize .val function, not property:

data: { 'cmd' : 'getinfo', 'tagid' : $('#tag').val() },

php jquery ajax curl

No comments:

Post a Comment